after auto logout how to redirect the control to l

2019-07-31 02:58发布

I am using below code to auto logout after some time interval

'session' => [
    'timeout' => 10,
],
'user' => [
    'identityClass' => 'common\models\User',
    'enableAutoLogin' => false,
    'authTimeout' => 10,
],

It logged out successfully but did not redirect to login page how to do that?

标签: yii2
1条回答
\"骚年 ilove
2楼-- · 2019-07-31 03:43

You can use behaviors for actions.

 public function behaviors()
 {
     return [
         'access' => [
             'class' => AccessControl::className(),
             'only' => ['index', 'logout','view','create','update','delete'],
             'rules' => [
                 [
                     'actions' => ['index', 'logout','view','create','update','delete'],
                     'allow' => true,
                     'roles' => ['@'],
                 ],
             ],
         ],
     ];
 }

So user is not logged in it will redirect to login url. You can also set login url

'session' => [
        'timeout' => 10,
      ],
'user' => [
      'identityClass' => 'common\models\User',
      'enableAutoLogin' => false,
      'authTimeout' => 10,
      'loginUrl' => 'admin/default/login' // set your login path here
     ],

OR You can use beforeAction method to check that user is logged in or not and send user to login page.

 public function beforeAction($action){

        if (Yii::$app->user->isGuest){
            return $this->redirect(['site/login'])->send();  // login path
        }
    }
查看更多
登录 后发表回答