Overriding base controller

Hello, in Yii 1 there was a base controller class in protected/components/Controller.php

Since all controllers (by default) extended from this class, this was useful to me, fos example, having a default layout for all controllers:


public $layout='//layouts/default';

Or do user verification overriding this function:


protected function beforeAction($action)

How can we achieve this in Yii 2?

Thank you!

Easy.

Nearly like in Yii 1.

Create a “BaseController” your own. ;)

Create for example:




use Yii;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\data\ActiveDataProvider;


class MyBaseController extends Controller

{

  // base controller code

}



And let all controllers inherit from that controller with:




use app\models\controllers\MyBaseController; 


class MyController extends MyBaseController

{

  // code

}



Regards

Thank you very much MetaCrawler for pointing me the right direction. That makes sense ;)