我们如何在CakePHP中提供身份验证?

我们如何在CakePHP中提供身份验证?,php,cakephp,Php,Cakephp,我正在学习CakePHP。在控制器中,我编写了这样一个程序。我还在登录页面上创建了一个表单。但是它显示了$this->Auth->login()的一个错误 如果我在CakePHP中这样给出它,它将显示以下错误: Error: Call to a member function login() on a non-object File: C:\wamp\www\cakephp\app\Controller\UsersController.php Line: 20 UserController.p

我正在学习CakePHP。在控制器中,我编写了这样一个程序。我还在登录页面上创建了一个表单。但是它显示了
$this->Auth->login()的一个错误

如果我在CakePHP中这样给出它,它将显示以下错误:

Error: Call to a member function login() on a non-object
File: C:\wamp\www\cakephp\app\Controller\UsersController.php
Line: 20
UserController.php
将此添加到您的
AppController.php

class AppController extends Controller {
    public $components = array(
                    'Auth' => array(
                    'loginAction' => array('controller'=>'User', 'action'=>'login'),
            'loginRedirect' => array('contoller'=>'User', 'action'=>'login'),
            'logoutRedirect' => array('controller'=>'User', 'action'=>'logout'),
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'User',
                    'fields' => array(
                        'username' => 'email',
                        'password'=>'password'
                    )))));
public function beforeFilter(){
            parent::beforeFilter();
            $this->Auth->allow('login');
        }
public function beforeSave($options = array()) {
        parent::beforeSave($options = array());
        if (isset($this->data['User']['password'])) {
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        return true;
    }
UserController.php

class AppController extends Controller {
    public $components = array(
                    'Auth' => array(
                    'loginAction' => array('controller'=>'User', 'action'=>'login'),
            'loginRedirect' => array('contoller'=>'User', 'action'=>'login'),
            'logoutRedirect' => array('controller'=>'User', 'action'=>'logout'),
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'User',
                    'fields' => array(
                        'username' => 'email',
                        'password'=>'password'
                    )))));
public function beforeFilter(){
            parent::beforeFilter();
            $this->Auth->allow('login');
        }
public function beforeSave($options = array()) {
        parent::beforeSave($options = array());
        if (isset($this->data['User']['password'])) {
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        return true;
    }
app/Model/User.php

class AppController extends Controller {
    public $components = array(
                    'Auth' => array(
                    'loginAction' => array('controller'=>'User', 'action'=>'login'),
            'loginRedirect' => array('contoller'=>'User', 'action'=>'login'),
            'logoutRedirect' => array('controller'=>'User', 'action'=>'logout'),
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'User',
                    'fields' => array(
                        'username' => 'email',
                        'password'=>'password'
                    )))));
public function beforeFilter(){
            parent::beforeFilter();
            $this->Auth->allow('login');
        }
public function beforeSave($options = array()) {
        parent::beforeSave($options = array());
        if (isset($this->data['User']['password'])) {
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        return true;
    }

你加载了Auth组件了吗?没有,请告诉我。