Yii2 check if user is logged before every page [duplicate]

。_饼干妹妹 提交于 2020-01-03 06:35:49

问题



I am new to Yii2.
How can i check if user is logged in or not?
If user is not logged in then it should redirect to login page.
Is there any global solution?


回答1:


I set the 'access' behaviors in all controllers

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['logout', 'signup'],
            'rules' => [
                [
                    'actions' => ['signup'],
                    'allow' => true,
                    'roles' => ['?'],
                ],
                [
                    'actions' => ['logout'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}

or check the Yii::$app->user->isGuest value



来源:https://stackoverflow.com/questions/31019339/yii2-check-if-user-is-logged-before-every-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!