[英]CakePHP login method read session if exists for redirect
我禁用了autoRedirect
因此我可以在用户控制器的登录方法中做一些额外的爵士工作,并使用Session将其发送回它们的来源。
class UsersController extends AppController
{
var $name = 'Users';
var $components = array('Session');
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow(array('login','logout','admin_logout','admin_login'));
$this->Session->write('back_to', $this->referer());
}
/**
* Log in
*/
function admin_login ()
{
$this->set('title_for_layout', 'Log in – Admin —');
if(!(empty($this->data)) && $this->Auth->user())
{
$back_to = $this->Session->read('back_to');
if($back_to)
{
$this->redirect($back_to, null, true);
}
else
{
$this->redirect($this->Auth->redirect(), null, true);
}
}
if($this->Auth->user())
{
$this->redirect($this->Auth->redirect(), null, true);
}
}
因此,想法是,如果用户拥有会话(99.%的时间),则在提交表单时它将把用户发送到上一页,如果没有,则发送到默认的loginRedirect。
注意:通过将autoRedirect
设置为false,CakePHP将不再使用Auth.Redirect
会话! 因此,存储在此处的值已不再被应用程序使用,这是故意的!
我遇到的问题是,由于上面代码中“此人”注释下面的功能,我的应用程序始终会将用户发送到仪表板。 如果我删除该功能,那么用户将一直被返回到登录表单,但他们将一直登录!
有人可以帮忙吗?
谢谢
更新:这是我的appcontroller代码:
class AppController extends Controller
{
var $components = array('Auth','Session');
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->authorize = 'controller';
$this->Auth->autoRedirect = false;
$this->Auth->loginAction = array('controller'=>'users','action'=>'login','admin'=>true
);
$this->Auth->loginRedirect = array('admin'=>true,'controller' => 'dashboard', 'action' => 'index');
$this->Auth->logoutRedirect = array('admin'=>false,'controller' => 'pages', 'action' => 'display','home');
}
function isAuthorized()
{
return true;
}
}
重定向后您不存在。 尝试将重定向签名更改为:
$this->redirect( $back_to, null, true );
第二个参数是状态码,第三个参数是是否停止处理当前动作。 这应该可以防止您下降到我猜测是正在执行的最后一个重定向。
鉴于下面我们的长长的注释“讨论”,请尝试调整admin_login()
方法,如下所示:
if(!(empty($this->data)) && $this->Auth->user()) {
$back_to = $this->Session->read('back_to');
if($back_to) {
$this->redirect($back_to, null, true);
}
else {
$this->redirect($this->Auth->redirect(), null, true);
}
}
else {
# Only write back_to when you arrive at the login page without data
$this->Session->write('back_to', $this->referer());
}
function login (){ if(!empty($this->data) && !$this->Auth->user()){ $this->Session->write('back_to', $this->referer()); }
删除该Session-> write在beforeFilter中。 而且您不需要$this->Auth->allow(array('login','logout'));
if(!(empty($this->data)) && $this->Auth->user())
{
$back_to = $this->Session->read('back_to');
$auth_redirect = $this->Session->read('Auth.redirect');
if($auth_redirect)
{
$this->redirect($auth_redirect, null, true);
}
else if($back_to)
{
$this->redirect($back_to, null, true);
}
else
{
$this->redirect($this->Auth->redirect(), null, true);
}
}
else
{
$this->Session->write('back_to', $this->referer());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.