我已禁用autoRedirect
,所以我可以在我的用户控制器的登录方法中做一些额外的爵士乐,并使用会话将它们发回他们来自的地方。CakePHP登录方法读会话如果存在重定向
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
会话!所以存储在那里的值不再被应用程序使用,并且是有意的!
我遇到的问题是我的应用总是将用户发送到仪表板,因为上面的代码中'This one'注释下面的功能。如果我删除了那个函数,那么用户只是一直发送回登录表单,但他们会被登录!
任何人都可以帮忙吗?
感谢
更新:这里是我的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;
}
}
根据这一页:http://book.cakephp.org/view/1270/loginRedirect你应该使用默认的重定向机制来做你想做的事情。除非你想要重定向,即使最后一次不需要登录... –