我读过一篇文章,其中展示了如何重定向到指定的登录页面。但是,我想要的是,如果用户来到前端,他们应该被重定向到前端登录页面,并且类似的应用在后端。可能吗?
在您的配置文件( main.php
) 上,请设置登录 URL。
// user
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
// set the url where user must be redirected if authentication needed
// use null to force 403 HTTP error
'loginUrl'=>'/site/login',
// set here the name of a class
// that extends CWebUser and it is stored in
// protected/components/<classname>
// see: http://www.yiiframework.com/doc/cookbook/60/
'class' => 'WebUser',
),
请参阅此网址:http://www.yiiframework.com/wiki/59/
然后在您的家庭控制器(SiteController.php
)上,请添加此代码,使您的索引主页只能由经过身份验证的用户访问。
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 'create' and 'update' actions
'actions'=>array('index', 'action1', 'action2', 'anotherAction'),
'users'=>array('@'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
请参阅此网址:http://www.yiiframework.com/doc/guide/1.1/en/topics.auth
最简单的方法应该是根据需要确定用户值
Yii::app()->getUser()->setState('LoginReturnUrl', 'frontend/login');
或
Yii::app()->getUser()->setState('LoginReturnUrl', 'backend/login');
并在控制器内部重定向:
$this->redirect(Yii::app()->createUrl(Yii::app()->getUser()->getState('LoginReturnUrl')));
'components' => [
...
'user' => [
'identityClass' => 'appmodelsUser',
'loginUrl' => [ 'YourControllerYourLogin' ],
],
]
在主.php。
loginUrl 必须用数组声明