- Yii2 By Example
- Fabrizio Caldarelli
- 515字
- 2021-07-16 19:38:55
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 to // class NewsController extends yii\web\Controller class NewsController extends Controller { // 4. actions are handled from controller functions whose name starts with 'action' and the first letter of each word is uppercase; public function actionIndex() { echo "this is my first controller"; } }
If we try to point the browser to http://hostname/basic/web/index.php?r=news/index
, we will see a blank page with the notice this is my first controller.
Now, let's see which common errors can occur when we ignore those four things to remember mentioned at the top of this chapter.
The namespace defines the hierarchical organization for names used in our application. If we forget to declare a namespace, Yii2 with YII_DEBUG
set to true in web/index.php
, will display the following error message:

The missing Controller namespace
Yii2 reports an error in an excellent way, giving us the possibility to solve it by checking if we are missing the namespace.
Then, the Use
keyword is employed to specify the complete path of a class in the application. A class that has a path/to/class/ClassName
complete path, can be referenced in the app using only ClassName
if we put an use path/to/class/ClassName
just after namespace declaration.
However, if we use just ClassName
without defining the use
declaration at the top of the file, an error such as the following can occur:

This error is simple to explain, but harder to find, especially for beginners.
In this case, the screenshot shows that it has been used the Controller
name (after the extends
keyword) at row 9. Since there is no complete path for the Controller
class name, Yii2 will try to look for the Controller
class under app\controllers
, without finding it.
To solve this problem, we must change Controller
with yii\web\Controller
at row 9 and for all the next rows that will use the Controller
class name without defining a complete class path, or that insert a use
declaration at the top of the file, we must employ yii\web\Controller
.
A controller is always a subclass of yii\web\Controller
or simply, if we have used the keyword use
, a subclass of Controller
. Action names follow the rules described in the previous chapter.