Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii::configure does not work in module init()

Tags:

php

yii2

I have a module calls API, and i want to load config file for it. The guide says that i have to use function \Yii::configure. I use it, but it doesn't apply any new configs. And i tried to use array instead config file, the result is same

class API extends \yii\base\Module
{
    public $controllerNamespace = 'api\client\controllers';

    public function init()
    {
        parent::init();

//        \Yii::configure($this, require(__DIR__ . '/config/main.php'));

        \yii::configure($this, [
            'components' => [
                'user' => [
                    'class' => 'yii\web\UserTest',
                    'identityClass' => 'api\client\models\User',
                ],
            ]
        ]);

        echo \yii::$app->user->className();

        die();
    }
}

How I can override config in my module ?

like image 397
RahulG Avatar asked Nov 27 '25 04:11

RahulG


1 Answers

UPDATE

You have to use setComponents method of Yii::$app

    Yii::$app->setComponents(
        [
            'errorHandler'=>[
                'errorAction'=>'forum/forum/error',
                'class'=>'yii\web\ErrorHandler',
            ],     
            'user' => [
                'class' => 'yii\web\User',
                'identityClass' => 'app\modules\profile\models\User',
            ],
        ]
    ); 

OLD ANSWER

Didn't it give you errors? Your casing are wrong and so instead of "yii" in small letters use "Yii" capitalized

class API extends \yii\base\Module
{
    public $controllerNamespace = 'api\client\controllers';

    public function init()
    {
        parent::init(); 

        \Yii::configure($this, [
            'components' => [
                'user' => [
                    'class' => 'yii\web\UserTest',
                    'identityClass' => 'api\client\models\User',
                ],
            ]
        ]);

        echo \Yii::$app->user->className();

        die();
    }
}
like image 125
Stefano Mtangoo Avatar answered Nov 28 '25 17:11

Stefano Mtangoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!