2011-11-02 22 views
5

tôi đã sử dụng hướng dẫn này: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.htmlGọi để một hàm thành viên cho phép() trên một tổ chức phi vật thể - cho phép

Để xây dựng hình thức đầu tiên của tôi/tạo ứng dụng người dùng, nhưng nó không thành công với một thông báo lỗi:

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

này IUS dòng 18:

$this->Auth->allow('add', 'logout'); 

dòng trên là một thành viên của chức năng:

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

My toàn 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')); 
    } 
} 
?> 

Tại sao nó happends?

Trả lời

13

Đảm bảo tính năng Auth compenent thực sự được gọi trong AppController của bạn. Nếu bạn không có một AppController tạo AppController.php trong thư mục Controller của bạn với đoạn mã sau:

<?php 
    class AppController extends Controller { 
    } 
?> 

Thành phần Auth được gọi trong một biến nào trong AppController, vì vậy bộ điều khiển sẽ trông như thế này:

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

Xác thực hiện khả dụng trong suốt ứng dụng của bạn. Bạn cũng có thể gọi AuthComponent trong UsersController của bạn, nhưng điều đó sẽ làm cho nó chỉ có sẵn cho bộ điều khiển cụ thể đó. Bạn có thể muốn sử dụng xác thực trong toàn bộ ứng dụng của mình.

+3

Chỉ cần bổ trợ cho những gì bạn đang nói. Trong CakePHP 2.0, AppController nằm trong thư mục Controllers và NOT trong thư mục app như trong 1.3. Điều này làm tôi rối tung lên, như tôi đã gọi thành phần, nhưng không phải là bản thân AppController thực sự! –

Các vấn đề liên quan