yii\web\Controller inheritance

Hello, All,

I’m using CURL requests in my Yii2 web application. They need switched off CSRF protection. The issue here is that the framework makes an instance of yii\web\Controller, before it makes an instance of my controller (for example app\controllers\GameController). The reason of this behavior is the two urlManager properties: “‘enableStrictParsing’ => true”, “‘showScriptName’ => false”. So, I cannot redeclare “public function beforeAction($action)” method for switching off CSRF protection ($this->enableCsrfValidation = false;) or (directly return “true” on specific controller, action, caller IP).

My question is:

  • How can I make a controller, which extends yii\web\Controller /I’ll call it “app\controllers\ApplicationController”/ and which the framework always calls instead of yii\web\Controller?

I have read many tutorials, forums, etc. I have not found my answer.

Thank you in advance!

Hristo Hristov

You need to have your custom application controller extend the default web controller and override settings in that. This controller will be your new base controller.

Then tell all your controllers to use that one instead of the default one.

New base controller





<?php


namespace app\controllers;


class ApplicationController extends \yii\web\Controller {

    //Overide default settings

}



All of your controllers would extend the above instead of the default one





<?php


namespace app\controllers;


use app\controllers\ApplicationController;


class GameController extends ApplicationController {

  

}