gpt4 book ai didi

forms - 在非对象上调用成员函数 allow() - 授权

转载 作者:行者123 更新时间:2023-12-04 16:55:18 25 4
gpt4 key购买 nike

我使用了这个教程:http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

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

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

我的整个 UsersController.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在您的 Controllers 目录中使用以下代码:

<?php
class AppController extends Controller {
}
?>

Auth 组件在 AppController 的公共(public)变量中调用,因此 Controller 看起来像这样:
<?php
class AppController extends Controller {
public $components = array('Auth');
}
?>

Auth 现在可在您的整个应用程序中使用。您也可以调用 AuthComponent在您的 UsersController 中,但这将使其仅可用于该特定 Controller 。您可能希望在整个应用程序中使用身份验证。

关于forms - 在非对象上调用成员函数 allow() - 授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7980518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com