我正在使用CakePHP的一个项目上工作,我有一个小问题。当未经身份验证的用户试图访问受限制的页面时,他们将被重定向到登录页面,这是使用Auth组件的CakePHP安全管理中的正常行为。
然而,一旦用户登录应用程序,它会将用户重定向到错误的url。也就是说,应用程序不是将用户重定向到它之前试图访问的url,而是将用户重定向到这样的url: localhost/myApplication/folder/myApplication/folder/urlUserTriedToAccess。
由于某种原因,项目的路径被写了两次,我不知道为什么。我希望有人能给我提示一下可能会发生什么。非常感谢!
这是我在AppController中设置Auth组件值的代码:
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'home'),
'authError' => "Debes iniciar sesión para entrar en esta página",
'authorize' => array('Controller') // Added this line
));
这是UsersController中Login动作中的代码:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Invalid user', 'default', array('class'=>'alert alert-error'));
}
}
}
您可以在登录后尝试下面的代码来验证重定向是否正常工作:
$this->redirect(array('controller'=>'anyRestrictedController','action'=>'index'));
另外,尝试使用redirectUrl()代替redirect()。