2013-03-15 65 views
1

我试着登录用户时收到以下错误致命错误:上将auth登录

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

我的登录操作使用外部模型。的动作是:被传递给$this->Auth->login

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

请求数据是:

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

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

有人可以请解释什么是错误意味着什么,并可能导致它?

+0

它在第二行上面列出: '调用成员函数登录()在非对象' – BIOS 2013-03-15 19:51:50

+0

你有没有包含Auth组件? – 2013-03-15 19:53:45

+0

刚刚注意到,为什么你明确加载你的用户模型? – 2013-03-15 19:56:26

回答

5

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

class UsersController extends AppController { 

    public $components = array(
     'Auth' 
    ); 

    public function login() { ... } 
} 

正如@thaJeztah指出 - 检查the docs正确用法,你的代码(这基础上,$this->request的使用,意味着你使用2.x的)是不正确的,不会测试用户是否存在并且可以登录 - 而是直接将用户登录到登录表单中。

+0

对不起。已更新问题。 – BIOS 2013-03-15 20:01:15