2011-11-02 59 views
5

我用這個教程: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 

這IUS的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')); 
    } 
} 
?> 

爲什麼可以出現?

回答

13

確保在您的AppController中實際調用Auth權限。如果你沒有一個AppController的在你的控制器用下面的代碼目錄中創建AppController.php

<?php 
    class AppController extends Controller { 
    } 
?> 

的驗證組件被稱爲在AppController的一個公共變量,因此控制器應該是這樣的:

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

Auth現在可在整個應用程序中使用。您也可以在您的UsersController中調用AuthComponent,但這隻會使該特定控制器可用。您可能想要在整個應用程序中使用身份驗證。

+3

只是你所說的插件。在CakePHP 2.0中,AppController位於Controllers文件夾中,而不在1.3中的應用程序文件夾中。這讓我感到困惑,因爲我正在調用組件,而不是實際的AppController本身! –

相關問題