Gii - Generating Controller



Let us see how to generate a Controller.

Step 1 − To generate a controller with several actions, open the controller generator interface fill in the form.

Generate Controller

Step 2 − Then, click the “Preview” button and “Generate”. The CustomController.php file with index, hello, and world actions will be generated in the controllers folder.

<?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');
      }
   }
?>

Form Generation

Step 1 − To generate a view file from an existing model, open the form generation interface and fill in the form.

Form Generation

Then, click the “Preview” button and “Generate”. The customview view file will be generated in the view folder.

Step 2 − To display it, add a new method to the CustomController.

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

Step 3 − To see the generated view file, open the URL http://localhost:8080/index.php?r=custom/view.

Generated View File
Advertisements