Forms 对非对象授权调用成员函数allow()

Forms 对非对象授权调用成员函数allow(),forms,cakephp,login,authorization,Forms,Cakephp,Login,Authorization,我使用了本教程: 要构建我的第一个表单/创建用户应用程序,但失败并显示错误消息: Fatal error: Call to a member function allow() on a non-object in /home/public_html/cake/app/Controller/UsersController.php on line 18 这是第18行: $this->Auth->allow('add', 'logout'); 上述行是函数的一个成员: public f

我使用了本教程:

要构建我的第一个表单/创建用户应用程序,但失败并显示错误消息:

Fatal error: Call to a member function allow() on a non-object in /home/public_html/cake/app/Controller/UsersController.php on line 18
这是第18行:

$this->Auth->allow('add', 'logout');
上述行是函数的一个成员:

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'logout');
}
我的整个
userscoontroller.php

<?php
class UsersController extends AppController {

    public function login() {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add', 'logout');
    }

    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'));
            $this->redirect(array('action'=>'index'));
        }
        $this->Session->setFlash(__('User was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
}
?>


为什么会发生这种情况?

确保在AppController中实际调用了Auth组件。如果没有AppController,请使用以下代码在控制器目录中创建
AppController.php

<?php
  class AppController extends Controller {
  }
?>

Auth组件在AppController中的公共变量中调用,因此控制器如下所示:

<?php
  class AppController extends Controller {
    public $components = array('Auth');
  }
?>


Auth现在在整个应用程序中都可用。您还可以在UsersController中调用
AuthComponent
,但这将使它仅对该特定控制器可用。您可能希望在整个应用程序中使用身份验证。

只是您所说内容的附加内容。在CakePHP 2.0中,AppController位于Controller文件夹中,而不是1.3中的app文件夹中。这把我搞砸了,因为我调用的是组件,而不是实际的AppController本身!