2

How to override/extend the namespace methods, with using \Yii::$classMap

I want to override/extend the beforeAction($action) method for yii\base\Controller, like this.

namespace yii\web;
class Controller extends \yii\web\Controller
{
    public function beforeAction($action)
    {
        return parent::beforeAction($action);
    }
    // Here all the parent methods in \yii\web\Controller should be available
}

And now in backend/web/index.php

\Yii::$classMap['yii\web\Controller'] = '@common/classMaps/Controller.php';

And now in backend/controllers/MyController,

namespace backend/controllers;
class MyController extends \yii\web\Controller
{
    public function actionIndex()
    {
    }
}

How it should be done??

  • I want not to change my `backend\controllers` code, I want to `override/extend` the `beforeAction($action)` method for `yii\base\Controller`. And tell the `YII`, that pick the `yii\base\Controller` along with my `override/extend` method. – Bilal Jafar Jan 02 '18 at 07:23
  • @kiamoz can you help some thing in this...??? – Bilal Jafar Jan 02 '18 at 07:26
  • In this way, I think you're trying to overwrite a class and then extend from it at the same time, don't think it's possible. You may have to extend your controllers from a separate class (that in turn extends yii\web\Controller). Also see https://github.com/yiisoft/yii2/issues/14465 – friek108 Feb 28 '18 at 10:50
  • I tried using `require_once(__DIR__.'/../../vendor/yiisoft/yii2/web/Controller');` inside the `backend/controllers/MyController` class and it was loading but not quite working... – friek108 Feb 28 '18 at 11:43
  • Perhaps try a variation of this answer: https://stackoverflow.com/questions/48056988/how-to-override-extend-the-namespace-methods-with-using-yiiclassmap (I can get it to work with \yii\db\ActiveQuery myself but not yii\db\Query) – friek108 Feb 28 '18 at 12:24

0 Answers0