YII Controllers

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Controllers

In MVC structure, controllers role is to process the requests from the user and generate the responses. The incoming requests are analyzed by the controllers, passed onto models, models results are directed into views and finally a response is generated.

Controllers Action

Controller contains actions which are called by user to execute a request. A controller can have more than one request.Following code is an example with two actions

Following code is an example with two actions update and create in a controller file SiteController.php.

 
  1. <?php   
  2.   
  3. namespace frontendcontrollers;   
  4.   
  5. use Yii;   
  6. use frontendmodelsYiicrud;   
  7. use frontendmodelsSearchYiicrud;   
  8. use yiiwebController;   
  9. use yiiwebNotFoundHttpException;   
  10. use yiifiltersVerbFilter;   
  11.   
  12. /**  
  13.  * YiicrudController implements the CRUD actions for Yiicrud model.  
  14.  */   
  15. class YiicrudController extends Controller   
  16. {   
  17.     /**  
  18.      * @inheritdoc  
  19.      */   
  20.     public function behaviors()   
  21.     {   
  22.         return [   
  23.             'verbs' => [   
  24.                 'class' => VerbFilter::className(),   
  25.                 'actions' => [   
  26.                     'delete' => ['POST'],   
  27.                 ],   
  28.             ],   
  29.         ];   
  30.     }  
  31. public function actionUpdate($id)   
  32.     {   
  33.         $model = $this->findModel($id);   
  34.   
  35.         if ($model->load(Yii::$app->request->post()) && $model->save()) {   
  36.             return $this->redirect(['view''id' => $model->id]);   
  37.         } else {   
  38.            return $this->render('update', [   
  39.                 'model' => $model,   
  40.             ]);   
  41.         }   
  42.     }   
  43.   
  44.     /**  
  45.      * Deletes an existing Yiicrud model.  
  46.      * If deletion is successful, the browser will be redirected to the 'index' page.  
  47.      * @param integer $id  
  48.      * @return mixed  
  49.      */   
  50.     public function actionDelete($id)   
  51.     {   
  52.         $this->findModel($id)->delete();   
  53.   
  54.         return $this->redirect(['index']);   
  55.     }  

Look at the above code, action update (actionUpdate( )), it will first load the model according to requested id, then tries to include the new model instance using the request data and save the model. Then it will be redirected to view action with the model id. Otherwise, it will return the update action.

Action delete (actionDelete( )), it will load the model according to requested id,and then delete it. It will be redirected to the index action.

Routes

In Yii URL, you must have noticed there is a r. This r is the route.

For example: http://localhost/index.php?r=site/index

The route is site/index in the above example.

which consist of the following parts:

moduleID : It applies only when controller belongs to a non-application module.

controllerID : A string that identifies the controller among all controllers within a same module. In above example, it is site.

actionID : A string that identifies the action name among all actions in a controller. In above example, it is index.

Route format is:

ControllerID/ActionID

If belongs to a module, it takes the following format:

  1. ModuleID/ControllerID/ActionID