身份验证登录时出现致命错误

时间:2013-03-15 19:50:45

标签: cakephp authentication cakephp-appmodel

尝试登录用户时出现以下错误:

Call to a member function login() on a non-object

我的登录操作使用外部模型。行动是:

public function login() {

        $this->loadModel('User');

        if ($this->request->is('post')) {

           $theUser = $this->User->find('first', array('conditions' => array('User.username' => $this->request->data['User']['username'])));    

            if($theUser['User']['activated'] == TRUE){

                if ($this->Auth->login($this->request->data)){

                   $this->Session->setFlash('Logged in successfully');

                    $this->redirect(array('controller' => 'admin', 'action' => 'index'));
                } else {

                    $this->Session->setFlash('Username or password is incorrect');
                }
            }else $this->Session->setFlash('User not yet activated. Please Contact administrator.');
        }
    }

传递给$this->Auth->login的请求数据是:

array(
    'User' => array(
        'password' => '*****',
        'username' => 'admin'
    )
)

$this->Auth->login($this->request->data)是导致致命错误的行。

有人可以解释错误的确切含义以及可能导致错误的原因吗?

1 个答案:

答案 0 :(得分:5)

您需要检查是否已在控制器中包含Auth组件。例如:

class UsersController extends AppController {

    public $components = array(
        'Auth'
    );

    public function login() { ... }
}

正如@thaJeztah指出的那样 - 检查the docs是否正确使用,因为您的代码(基于$this->request的用法,暗示您使用的是2.x)是不正确的不测试用户是否存在并且可以登录 - 而是直接将用户登录到登录表单中的任何内容。

相关问题