让我们来看看如何使用 Gii 来生成一个控制器。
第1步 - 生成具有几个操作的控制器,打开控制器生成接口填写表单字段内容,如下:
第2步 - 然后单击 “Preview” 和 “Generate” 按钮。自定义 CustomController.php 文件会在 controllers 文件夹中生成,CustomController 控制器中同时定义有:index, hello 和 world 动作这几个动作。
确定生成结果如下代码:
<?php
namespace app\controllers;
class CustomController extends \yii\web\Controller {
public function actionHello() {
return $this->render('hello');
}
public function actionIndex() {
return $this->render('index');
}
public function actionWorld() {
return $this->render('world');
}
}
?>
表单生成
第1步 - 要从现有模型生成视图文件,打开表单生成接口并填写表单字段,如下所示:

然后,单击 “Preview” 和 “Generate” 按钮。自定义视图 customview 文件将在视图文件夹(views)中生成。


第2步 - 要显示它,添加一个新的方法到 CustomController 控制器。
public function actionView() {
$model = new MyUser();
return $this->render('/customview', [
'model' => $model,
]);
}
上一篇:
Yii Gii创建模型
下一篇:
Yii Gii创建模块

