Redirect 如果用户未登录,如何重定向yii中的登录页面?
如何检查每个页面上的用户是否已登录,如果用户未登录,则必须重定向到登录页面。Redirect 如果用户未登录,如何重定向yii中的登录页面?,redirect,yii,Redirect,Yii,如何检查每个页面上的用户是否已登录,如果用户未登录,则必须重定向到登录页面。 为了访问任何页面,用户必须执行身份验证。Yii为操作提供访问控制,以下是示例。尝试此操作,如果来宾用户尝试访问这些操作,它们将被重定向到登录页面 class ExampleController extends Controller { /** * @return array action filters */ public function f
为了访问任何页面,用户必须执行身份验证。Yii为操作提供访问控制,以下是示例。尝试此操作,如果来宾用户尝试访问这些操作,它们将被重定向到登录页面
class ExampleController extends Controller {
/**
* @return array action filters
*/
public function filters() {
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules() {
return array(
array('allow', // allow authenticated user to perform these actions
'actions' => array('create','delete'), // add the actions need authuentication
'users' => array('@'),
),
);
}
}
在每个控制器中添加此功能
public function beforeAction($action) {
if (Yii::app()->user->isGuest && Yii::app()->controller->action->id != "login") {
Yii::app()->user->loginRequired();
}
//something code right here if user valid
return true;
}
站点控制器只需要
Yii::app()->controller->action->id!=“登录”
此条件。为其他控制器删除它。我认为这在ExampleController下可以工作。我想,如果用户键入任何url,必须重定向到登录页面只有授权用户才能访问页面或执行任何操作OK。您的所有控制器都继承//扩展控制器{//,在那里编写这两个函数。希望此工作当前我在操作($action){if(Yii::app()->user->isGuest){$this->redirect(Yii::app()->createUrl('user/login/login');返回true;}否则{return true;}//如果用户有效返回true,则此处有一些代码;}