Override component in module does not work

When I print \Yii::$app->user->loginUrl inside the module, I expect to get an array of [‘admin/security/signin’], instead I am getting [‘security/signin’] that I defined in config/web.php.




// modules/admin/Module.php


namespace app\modules\admin;


class Module extends \yii\base\Module

{

    public $layout = 'main.html.twig';


    public function init()

    {

        parent::init();


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


        echo '<pre>';

        print_r(\Yii::$app->user->loginUrl);

        echo '</pre>';

        die();

    }

}


// modules/admin/config.php


return [

    'components' => [

        'request' => [

            'class' => 'yii\web\Request',

            'cookieValidationKey' => 'uBv%mv-XfHc1/.q4<gOjHM9uC@q*oF%EIB@ntE-|6P*tV#Y&1gt=#[3mNZ(pHs,K)',

            'csrfParam' => '_adminCsrf',

        ],

        'user' => [

            'class' => 'yii\web\User',

            'identityClass' => 'app\modules\admin\models\Admin',

            'enableAutoLogin' => true,

            'identityCookie' => [

                'name' => '_adminIdentity',

            ],

            'loginUrl' => ['admin/security/signin'],

        ],

        'session' => [

            'class' => 'yii\web\Session',

            'name' => 'PHPADMINSESSID',

            'savePath' => sys_get_temp_dir(),

        ],

    ],

];


// config/web.php


$config = [

    'components' => [

        'request' => [

            'cookieValidationKey' => '[VsKM+=Q|zte&85AkP|3Ao7d-Jjd:EX_8J/R?wW~HtNjhQMj=5%Y7>|vvMwLZGEW',

            'csrfParam' => '_csrf',

        ],

        'user' => [

            'identityClass' => 'app\models\User',

            'enableAutoLogin' => true,

            'identityCookie' => [

                'name' => '_identity',

            ],

            'loginUrl' => ['security/signin'],

        ],

        'session' => [

            'name' => 'PHPSESSID',

            'savePath' => sys_get_temp_dir(),

        ],

    ],

    'modules' => [

        'admin' => [

            'class' => 'app\modules\admin\Module',

        ],

    ],

];



Do it this way:





public function init()

{

	parent::init();

	$this->registerComponents();

}


public function registerComponents(){

	\Yii::$app->setComponents([

		'user' => [

			'class'=>'yii\web\User',

			'identityClass' => 'app\modules\admin\models\AdminUser',

			'enableAutoLogin' => false,

			'loginUrl'=>Url::to(["/admin/dashboard/login"])

		],

	]);

}

Thanks! I forgot that’s how I did in Yii 1.x. Do you know where can I find the reference for this method in the documentation?

http://www.yiiframework.com/doc-2.0/yii-di-servicelocator.html

Paste on this anchor tag separately …

#setComponents()-detail

(The forums link code doesn’t like the () in the url )