cakePHP-设置loginRedirect从beforeFilter为admin角色 [英] cakePHP- setting loginRedirect from beforeFilter for admin role
问题描述
我面对这个奇怪的问题。我试图更改admin角色的默认loginRedirect从正常用户的。
U在AppController的组件变量中设置了auth键,如下所示:
'Auth'=> array(
'loginRedirect'=> array(
'controller'=>'donor',
'action'=>'index'
)
)
现在在beforeFilter回调中我有这个设置:
if($ this-> Auth-> user('role')=='admin'){
$ this-& > loginRedirect = array(
'controller'=>'users',
'action'=>'admin_index',
'prefix'=>'admin',
'admin'=> true
);
}
然而,这不工作,if条件永远不会满足。我希望这可以在用户登录时运行。如果我添加 else
条件并重复上面显示的相同的代码,它工作,管理员重定向到所需的页面。
任何人都可以指导我如何正确地做到这一点?提前感谢
如果用户未登录, $ this-> Auth-> ; user()
将返回null。 beforeFilter()将在任何操作运行之前运行,因此您的login()操作仍未被调用。
在执行 $ this-> Auth-> login()
后重新导向。例如。在您的 UsersController :: login()
操作(或您用于登录的任何操作):
if($ this-> Auth-> login()){
if($ this-> Auth-> user('role')=='admin'){
$ this-> redirect(array(
'controller'=>'users',
'action'=>'admin_index',
'prefix'=> ;'admin',
'admin'=> true
);
}
}
I am facing this weird problem. I am trying to change the default loginRedirect of the admin role from that of normal user.
U have the auth key in the AppController's component variable set up as follows :
'Auth' => array( 'loginRedirect' => array( 'controller' => 'donors', 'action' => 'index' ) )
Now in the beforeFilter callback I have this set up:
if($this->Auth->user('role') == 'admin'){ $this->Auth->loginRedirect = array( 'controller'=>'users', 'action'=>'admin_index', 'prefix'=>'admin', 'admin'=>true ); }
However, this does not work and the if condition is never met. I am expecting this to run when the user logs in. If I add an
else
condition and repeat the same code shown above, it works and the admin is redirect to the desired page.Can anyone instruct how I am able to do this correctly ? Thanks in advance
解决方案If the user is not logged in,
$this->Auth->user()
will return null. beforeFilter() will run before any action is run, so your login() action has still not been called.Do the redirecting after
$this->Auth->login()
has been called and is successful. E.g. in yourUsersController::login()
action (or whichever action you use to login):if ($this->Auth->login()) { if($this->Auth->user('role') == 'admin') { $this->redirect(array( 'controller'=>'users', 'action'=>'admin_index', 'prefix'=>'admin', 'admin'=>true ); } }
这篇关于cakePHP-设置loginRedirect从beforeFilter为admin角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!