Yii2检查用户是否在每个页面之前都已登录 [英] Yii2 check if user is logged before every page
本文介绍了Yii2检查用户是否在每个页面之前都已登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Yii2的新手.
如何检查用户是否登录?
如果用户未登录,则应重定向到登录页面.
有什么全球性的解决方案吗?
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?
推荐答案
我在所有控制器中设置了访问"行为
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'],
],
],
];
}
或检查Yii::$app->user->isGuest
值
这篇关于Yii2检查用户是否在每个页面之前都已登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文