如何使用 CakePHP 2.x 获取被 Auth 组件拒绝访问的页面?如果我使用 referer() 函数,它会给我链接到被拒绝操作的页面。这是我的代码:
public function login() {
//get the current url of the login page (or current controller+action)
$currentLoginUrl = "/login";
if ($this->request->is('post')) {
$this->User->recursive = -1;
$user = $this->User->find(
'first',
array(
'conditions' => array(
'User.username' => $this->request->data['User']['username'],
'User.password' => AuthComponent::password($this->request->data['User']['password'])
)
)
);
if ($user && $this->Auth->login($user['User'])) {
//if the referer page is not from login page,
if( $this->referer() != $currentLoginUrl )
//use $this->referer() right away
$this->redirect($this->referer('/admin', true)); //if referer can't be read, or if its not from local server, use $this->Auth->rediret() instead
else
//if the user lands on login page first, rely on our session
$this->redirect( $this->Session->read('beforeLogin_referer') );
}
else
$this->Session->setFlash('Username or password is incorrect', 'default', array('class' => 'alert-danger'));
}
if( $this->referer() != $currentLoginUrl )
//store this value to use once user is succussfully logged in
$this->Session->write('beforeLogin_referer', $this->referer('/admin', true) ) ; //if referer can't be read, or if its not from local server, use $this->Auth->rediret() instead
}
所以基本上发生的事情是我没有登录,我在这个网址:
'http://localhost/hotelguide/hotels/view/17/'
然后我点击一个链接,将我带到
'http://localhost/hotelguide/hotels/review/17/'
但这需要用户登录,因此它将我重定向到登录页面,当我调试referrer()时,它给了我这个:
'http://localhost/hotelguide/hotels/view/17/'
我究竟做错了什么?