我正在尝试创建一个非常简单的链接,将用户发送到登录页面。这是我的链接代码:
echo $this->Html->link('Login', array('controller' => 'Users', 'action' => 'login'));
这很好,用户会被发送到登录页面,但问题是,一旦用户登录,他们就会被引导回登录页面!
我知道问题是cakeHP会自动将用户重定向到最后一个页面,这是一件好事,在这个例子中,用户试图在没有登录的情况下访问某个页面,然后一旦他们登录,它就会将他们重定向到该页面。但是,我如何创建一个简单的登录页面链接,在他们登录后重定向到他们点击登录链接时所在的页面?
这是我的登录功能:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$returned = $this->Session->read('referedLogin');
return $this->redirect($this->Auth->redirect($this->Session->read('referedLogin')));
}
$this->Session->setFlash(__('Invalid username or password, try again'));
} else {
$this->Session->write('referedLogin', $this->referer());
}
}
在AppController中添加loginRedirect
链接
public $components = array('Auth' => array(
'loginAction' => array('controller'=>'User', 'action'=>'login'),
'loginRedirect' => array('contoller'=>'User', 'action'=>'login'),
'logoutRedirect' => array('controller'=>'User', 'action'=>'logout'),
..............................................
您必须将目标URL放入会话:
Auth.redirect
然后用户将被重定向到该URL。
我认为以下代码可能会解决您的问题,
在登录操作中添加以下代码,
if(!empty($this->Auth->user) {
$controller = $this->params['controller'];
$action = $this->params['action'];
return $this->redirect(array('controller' => $controller, 'action' => $action));
}