官术网_书友最值得收藏!

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:

Creating Controller and Action

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:

Creating Controller and Action

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.

主站蜘蛛池模板: 马公市| 衡山县| 调兵山市| 珲春市| 白玉县| 沿河| 唐海县| 苏尼特左旗| 台南县| 柳林县| 浦城县| 都江堰市| 鄂托克旗| 五家渠市| 安庆市| 衡南县| 望江县| 获嘉县| 湖南省| 冕宁县| 望谟县| 奉新县| 正阳县| 明光市| 鹤庆县| 屯昌县| 连城县| 赤水市| 铜山县| 周口市| 阳城县| 门源| 集安市| 澄迈县| 丹凤县| 灵璧县| 三门峡市| 汝阳县| 鄯善县| 裕民县| 临漳县|