简体   繁体   中英

How to change access controller redirect/login URL in Yii2?

I have two login systems in my Yii2 application. First is default login system using User table, and second uses sms_account table. In custom controller I've created action for login, actionLogin() . I've added access control for my custom controller, but I'm having problem that when a person is not logged in, it redirects to site/login . I want to change redirect to custom-controller/login URL in Yii2 access control. My code is:

public function behaviors()
{
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
        'access' => [
            'class'  => AccessControl::className(),
            'rules' =>  [
                [
                    'actions' => ['login'],
                    'allow' => false,
                    'roles' => ['@']
                ],
                [
                    'actions' => ['home'],
                    'allow' => true,
                    'roles' => ['@']
                ]
            ]
        ]
    ];
}

Can anyone tell me how to change access control URL?

You should simply configure your user component :

'user' => [
    // ...
    'loginUrl' => ['custom-controller/login'],
],

Read more about yii\\web\\User::$loginUrl .

And it should be :

[
    'actions' => ['login'],
    'allow' => true,
    'roles' => ['?']
],

Read more about Authorization in Yii2 .

 This is my updated answer . I thinks it's help you 
  public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'only' => ['index','logout','client-create'], // your own action which permission the login
                    'rules' => [
                        [
                            'actions' => ['index','logout','client-create'], // your own action which permission the login
                            'allow' => true,
                            'roles' => ['@'],
                        ],
                    ],
                    'denyCallback' => function($rule, $action) {
                     Yii::$app->response->redirect(['login/login']); 
                     },
                ],            
            ];
        }

Try this code. Note:roles

[
    'actions' => ['home'],
    'allow' => true,
    'roles' => ['?']
    'matchCallback' => function ($rule, $action) {
        return $this->redirect('index.php?r=controller/action');
    }
]

You should change it your-project/vendor/yiisoft/yii2/web/User.php line no 94

public $loginUrl = ['custom-controller/custom-action'];

and your behaviors code is given below

public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index','logout'], // allow your own action in controller
                'rules' => [
                    [                        
                        'allow' => true,
                        'roles' => ['@'],                        
                    ],
                ],
            ],

        ];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM