登录:
public function login(){
if($this->request->is('post')){
if($this->Auth->login($this->request->data)){
return $this->redirect($this->Auth->redirectUrl());
}else{
$this->Session->setFlash('Invalid Username or Password');
}
}
}
此返回值:
if(AuthComponent::user()
但当我使用这个:
if((AuthComponent::user('role'))==1)
它根本不返回值
我可以正常登录注销
对用户进行身份验证时,会按照附加的顺序检查附加的身份验证对象。一旦其中一个对象能够识别用户,就不会检查其他对象。使用登录表单的示例登录函数可能如下所示:
public function login() {
if ($this->request->is('post')) {
// Important: Use login() without arguments! See warning below.
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
// Prior to 2.3 use
// `return $this->redirect($this->Auth->redirect());`
}
$this->Session->setFlash(
__('Username or password is incorrect'),
'default',
array(),
'auth'
);
}
}
上面的代码(没有任何数据传递给登录方法)将尝试使用POST数据登录用户,如果成功,则将用户重定向到他们访问的最后一个页面,或AuthComponent::$loginDirect。如果登录不成功,则会设置一条闪烁消息。
在2.x中,$this->Auth->login($this->request->data)
将使用发布的任何数据登录用户,而在1.3中,$this->Auth->login($this->data)
将尝试首先识别用户,并仅在成功时登录。