Gii - 生成控制器



讓我們看看如何生成一個控制器。

步驟 1 - 要生成一個帶有若干操作的控制器,開啟控制器生成器介面,填寫表單。

Generate Controller

步驟 2 - 然後,點選“預覽”按鈕和“生成”。CustomController.php 檔案以及 index、hello 和 world 操作將生成在 controllers 資料夾中。

<?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 - 要從現有模型生成檢視檔案,開啟表單生成器介面並填寫表單。

Form Generation

然後,點選“預覽”按鈕和“生成”。customview 檢視檔案將生成在 view 資料夾中。

步驟 2 - 要顯示它,請向CustomController新增一個新方法。

public function actionView() {
   $model = new MyUser();
   return $this->render('/customview', [
      'model' => $model,
   ]);
}

步驟 3 - 要檢視生成的檢視檔案,開啟 URL https://:8080/index.php?r=custom/view

Generated View File
廣告