In this tutorial, we will go through the following:

  • Creating controller and action
  • Creating a view to display the news list
  • How the controller sends the data to view
    • Example – create a controller to display the static news items list and details
  • Split the common view content into reusable views
    • Example – render partial in view
  • Creating static pages
  • Share data between views and layout
    • Example – change layout background based on the URL parameter
  • Layout with dynamic blocks
    • Example – add dynamic box to display advertising info
  • Using multiple layouts
    • Example – using different layout to create responsive and not responsive layout for the same view

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 appcontrollers)
  • The use path for used class
  • The controller class must extend the yiiwebController 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:


// 1. specify namespace at the top (in basic application usually appcontrollers);
namespace appcontrollers;

// 2. specify 'use' path for used class;
use Yii;
use yiiwebController;

// 3. controller class must extend yiiwebController class;
// This line is equivalent to
// class NewsController extends yiiwebController
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 appcontrollers, without finding it.

To solve this problem, we must change Controller with yiiwebController 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 yiiwebController.

A controller is always a subclass of yiiwebController or simply, if we have used the keyword use, a subclass of Controller.