首页 > 解决方案 > yii2 会话超时后将任何页面重定向到登录页面

问题描述

我想问一下yii2会话超时后将任何页面重定向到登录页面,我尝试这样做

        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => false,
            'returnUrl' => 'index.php?r=site/login',
            'identityCookie' => [
                'name' => '_backendIdentity',
            ],
        ],

        'session' => [
            'name' => 'BACKENDSESSID',
            'savePath' => __DIR__ . '/../runtime',
        ],

但是当我在 index.php 页面时它会返回登录页面,如果我去另一个动作并且会话超时它将返回

PHP 通知 – yii\base\ErrorException Trying to get property of non-object

1. in /var/www/html/spmoa/backend/controllers/StaffController.php at line 174
165166167168169170171172173174175176177178179180181182183

    public function actionMyProfile() {
       error here ->  $id = Yii::$app->user->identity->staff->id;
        $model = $this->findModel($id);

请帮我

标签: phpyii2

解决方案


尝试行为

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [

                [
                    'actions' => ['youraction'],
                    'allow' => true,
                    'roles' => ['@'], // you can use matchCallback to create  more powerful  check 
                ],
            ],
        ], 

    ];`enter code here`
}

推荐阅读