Creating Controller and Action
In order to handle a request, the first thing to do is to create a new controller.
The things you must remember while creating a file controller are as follows:
- The namespace at the top (in basic application usually app\controllers)
- The
use
path for used class - The controller class must extend the
yii\web\Controller
class - The actions are handled from controller functions whose name starts with
action
and the first letter of each word is in uppercase
Let's point to basic/controllers
and create a file named NewsController.php
.
Then, create a class with the same name as the file and extend it from controller; finally, create an action named index
to manage request for news/index
:
<?php // 1. specify namespace at the top (in basic application usually app\controllers); namespace app\controllers; // 2. specify 'use' path for used class; use Yii; use yii\web\Controller; // 3. controller class must extend yii\web\Controller class; // This line is equivalent...