(cakephp中的AuthComponent :: user('role')没有在cakephp中正确返回数据

时间:2015-03-28 10:31:28

标签: php cakephp

登录:

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)

它根本不会返回值

我可以正常登录注销

1 个答案:

答案 0 :(得分:0)

对用户进行身份验证时,将按附加顺序检查附加的身份验证对象。一旦其中一个对象可以识别用户,则不会检查其他对象。使用登录表单的示例登录功能可能如下所示:

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'
        );
    }
}

上面的代码(没有传递给login方法的任何数据)将尝试使用POST数据记录用户,如果成功将用户重定向到他们访问的最后一页,或者AuthComponent :: $ loginRedirect 。如果登录失败,则会设置闪存消息。

在2.x $this->Auth->login($this->request->data)中将使用发布的任何数据记录用户,而在1.3 $this->Auth->login($this->data)中将首先尝试识别用户,并且仅在成功时登录。