2011-11-02 8 views
5

que utiliza este tutorial: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.htmlllamada a una función miembro permitir que() en un no-objeto - Autorización

para construir mi primera forma/Crear aplicación de usuario, pero falla con un mensaje de error:

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

Este iUS la línea 18:

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

la línea anterior es un miembro de la función:

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

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

¿Por qué happends?

Respuesta

13

Asegúrese de que el Auth compenent se llame realmente en su AppController. Si usted no tiene un AppController crear AppController.php en el directorio de controladores con el siguiente código:

<?php 
    class AppController extends Controller { 
    } 
?> 

el componente Auth se llama en una variable pública en el AppController, por lo que el controlador se vería así:

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

Auth ya está disponible en toda la aplicación. También puede llamar al AuthComponent en su UsersController, pero eso lo haría solo disponible para ese controlador en particular. Probablemente desee utilizar la autenticación en toda su aplicación.

+3

Solo un complemento a lo que dices. En CakePHP 2.0, el AppController está en la carpeta Controladores y NO en la carpeta de la aplicación como en 1.3. Esto me molestó, ya que estaba llamando al componente, ¡pero no al propio AppController! –

Cuestiones relacionadas