Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 how change loginUrl in components user for module

Tags:

php

yii2

I have following config a component user

    'user' => [
        'identityClass' => 'app\models\web\User',
        'enableAutoLogin' => true,
        'loginUrl'=>['/backend/login'],
    ],

I have 2 modules backend and frontend. I want if user go to the backend part need use following rule 'loginUrl'=>['/backend/login'] and if to thefrontend part this rule 'loginUrl'=>['/frontend/login']. How can I do that?

like image 393
Jack Avatar asked Nov 28 '25 01:11

Jack


1 Answers

For any module or controller you can redirect to your login action using AccessControl behavior in module or controller:

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => ['login'],
                    'allow' => true,
                    'roles' => ['?'],
                ],
                [
                    'actions' => ['index'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
            'denyCallback' => function($rule, $action) {
                return Yii::$app->response->redirect(['/your/login/url']);
            },
        ],
    ];
}

Or in Module class file:

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

    Yii::$app->user->loginUrl = ['/your/login/url'];
}
like image 111
pa3py6aka Avatar answered Nov 30 '25 13:11

pa3py6aka



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!