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

BIO*_*IOS 1 authentication cakephp cakephp-model

我在尝试登录用户时收到以下错误:

Call to a member function login() on a non-object
Run Code Online (Sandbox Code Playgroud)

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

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.');
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

array(
    'User' => array(
        'password' => '*****',
        'username' => 'admin'
    )
)
Run Code Online (Sandbox Code Playgroud)

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

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

AD7*_*six 5

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

class UsersController extends AppController {

    public $components = array(
        'Auth'
    );

    public function login() { ... }
}
Run Code Online (Sandbox Code Playgroud)

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