Class yii\gii\controllers\DefaultController
Inheritance | yii\gii\controllers\DefaultController » yii\web\Controller |
---|---|
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-gii/blob/master/src/controllers/DefaultController.php |
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$generator | yii\gii\Generator | yii\gii\controllers\DefaultController | |
$layout | yii\gii\controllers\DefaultController | ||
$module | yii\gii\Module | yii\gii\controllers\DefaultController |
Public Methods
Method | Description | Defined By |
---|---|---|
actionAction() | Runs an action defined in the generator. | yii\gii\controllers\DefaultController |
actionDiff() | yii\gii\controllers\DefaultController | |
actionIndex() | yii\gii\controllers\DefaultController | |
actionPreview() | yii\gii\controllers\DefaultController | |
actionView() | yii\gii\controllers\DefaultController | |
beforeAction() | yii\gii\controllers\DefaultController |
Protected Methods
Method | Description | Defined By |
---|---|---|
loadGenerator() | Loads the generator with the specified ID. | yii\gii\controllers\DefaultController |
Property Details
Method Details
Runs an action defined in the generator.
Given an action named "xyz", the method "actionXyz()" in the generator will be called. If the method does not exist, a 400 HTTP exception will be thrown.
public mixed actionAction ( $id, $name ) | ||
$id | string |
The ID of the generator |
$name | string |
The action name |
return | mixed |
The result of the action. |
---|---|---|
throws | \yii\web\NotFoundHttpException |
if the action method does not exist. |
public function actionAction($id, $name)
{
$generator = $this->loadGenerator($id);
$method = 'action' . $name;
if (method_exists($generator, $method)) {
return $generator->$method();
}
throw new NotFoundHttpException("Unknown generator action: $name");
}
public void actionDiff ( $id, $file ) | ||
$id | ||
$file |
public function actionDiff($id, $file)
{
$generator = $this->loadGenerator($id);
if ($generator->validate()) {
foreach ($generator->generate() as $f) {
if ($f->id === $file) {
return $this->renderPartial('diff', [
'diff' => $f->diff(),
]);
}
}
}
throw new NotFoundHttpException("Code file not found: $file");
}
public void actionIndex ( ) |
public function actionIndex()
{
$this->layout = 'main';
return $this->render('index');
}
public void actionPreview ( $id, $file ) | ||
$id | ||
$file |
public function actionPreview($id, $file)
{
$generator = $this->loadGenerator($id);
if ($generator->validate()) {
foreach ($generator->generate() as $f) {
if ($f->id === $file) {
$content = $f->preview();
if ($content !== false) {
return '<div class="content">' . $content . '</div>';
}
return '<div class="error">Preview is not available for this file type.</div>';
}
}
}
throw new NotFoundHttpException("Code file not found: $file");
}
public void actionView ( $id ) | ||
$id |
public function actionView($id)
{
$generator = $this->loadGenerator($id);
$params = ['generator' => $generator, 'id' => $id];
$preview = Yii::$app->request->post('preview');
$generate = Yii::$app->request->post('generate');
$answers = Yii::$app->request->post('answers');
if ($preview !== null || $generate !== null) {
if ($generator->validate()) {
$generator->saveStickyAttributes();
$files = $generator->generate();
if ($generate !== null && !empty($answers)) {
$params['hasError'] = !$generator->save($files, (array) $answers, $results);
$params['results'] = $results;
} else {
$params['files'] = $files;
$params['answers'] = $answers;
}
}
}
return $this->render('view', $params);
}
public void beforeAction ( $action ) | ||
$action |
public function beforeAction($action)
{
Yii::$app->response->format = Response::FORMAT_HTML;
return parent::beforeAction($action);
}
Loads the generator with the specified ID.
protected yii\gii\Generator loadGenerator ( $id ) | ||
$id | string |
The ID of the generator to be loaded. |
return | yii\gii\Generator |
The loaded generator |
---|---|---|
throws | \yii\web\NotFoundHttpException |
protected function loadGenerator($id)
{
if (isset($this->module->generators[$id])) {
$this->generator = $this->module->generators[$id];
$this->generator->loadStickyAttributes();
$this->generator->load(Yii::$app->request->post());
return $this->generator;
}
throw new NotFoundHttpException("Code generator not found: $id");
}