Controllers are responsible for processing requests and generating responses. After user’s request, the controller will analyze request data, pass them to model, then insert the model result into a view, and generate a response.
Understanding Actions
Controllers include actions. They are the basic units that user can request for execution. A controller can have one or several actions.
Let us have a look at the SiteController of the basic application template ā
<?php namespace app\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; use app\models\LoginForm; use app\models\ContactForm; class SiteController extends Controller { public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'only' => ['logout'], 'rules' => [ [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['post'], ], ], ]; } public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], ]; } public function actionIndex() { return $this->render('index'); } public function actionLogin() { if (!\Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { return $this->goBack(); } return $this->render('login', [ 'model' => $model, ]); } public function actionLogout() { Yii::$app->user->logout(); return $this->goHome(); } public function actionContact() { //load ContactForm model $model = new ContactForm(); //if there was a POST request, then try to load POST data into a model if ($model->load(Yii::$app->request->post()) && $model>contact(Yii::$app->params ['adminEmail'])) { Yii::$app->session->setFlash('contactFormSubmitted'); return $this->refresh(); } return $this->render('contact', [ 'model' => $model, ]); } public function actionAbout() { return $this->render('about'); } public function actionSpeak($message = "default message") { return $this->render("speak",['message' => $message]); } } ?>
Run the basic application template using PHP built-in server and go to the web browser atĀ http://localhost:8080/index.php?r=site/contact. You will see the following page ā
When you open this page, the contact action of theĀ SiteControllerĀ is executed. The code first loads theĀ ContactFormĀ model. Then it renders the contact view and passes the model into it.
If you fill in the form and click the submit button, you will see the following ā
Notice that this time the following code is executed ā
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app>params ['adminEmail'])) { Yii::$app->session->setFlash('contactFormSubmitted'); return $this->refresh(); }
If there was a POST request, we assign the POST data to the model and try to send an email. If we success then we set a flash message with the text āThank you for contacting us. We will respond to you as soon as possible.ā and refresh the page.
Understanding Routes
In the above example, in the URL, http://localhost:8080/index.php?r=site/contact, the route is site/contact. The contact action (actionContact) in the SiteController will be executed.
A route consists of the following partsā
- moduleID ā If the controller belongs to a module, then this part of the route exists.
- controllerID (site in the above example) ā A unique string that identifies the controller among all controllers within the same module or application.
- actionID (contact in the above example) ā A unique string that identifies the action among all actions within the same controller.
The format of the route is controllerID/actionID. If the controller belongs to a module, then it has the following format: moduleID/controllerID/actionID.