0
votes

I am creating one login authentication app in CakePHP and getting this Fatal error: Call to a member function allow() on a non-object in /var/www/cakephp1/app/Controller/users_controller.php on line 5

and this is my controller code users_controller.php

<?php
class UsersController extends AppController {
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add');
    }
    public function add() {
        if (!empty($this->data)) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash('User created!');
                $this->redirect(array('action'=>'login'));
            } else {
                $this->Session->setFlash('Please correct the errors');
            }
        }
        $this->set('groups', $this->User->Group->find('list'));
    }
    public function login() {
    }
    public function logout() {
        $this->redirect($this->Auth->logout());
    }
    public function dashboard() {
        $groupName = $this->User->Group->field('name', 
            array('Group.id'=>$this->Auth->user('group_id'))
        );
        $this->redirect(array('action'=>strtolower($groupName)));
    }
    public function user() {
    }
    public function administrator() {
    }
    public function manager() {
    }
}
?>

app_controller.php

<?php
class AppController extends Controller {
    public $components = array(
        'Acl',
        'Auth' => array(
            'authorize' => 'actions',
            'loginRedirect' => array(
                'admin' => false,
                'controller' => 'users',
                'action' => 'dashboard'
            )
        ),
        'Session'
    );
}
?>

View login.ctp

<?php
echo $this->Form->create(array('action'=>'login'));
echo $this->Form->inputs(array(
    'legend' => 'Login',
    'username',
    'password',
    'remember' => array('type' => 'checkbox', 'label' => 'Remember me')
));
echo $this->Form->end('Login');
?>

I am using CakePHP version 1.3

3
Is your App Controller in the app/ directory like it should be in Cake1.3 ? - Dave
@Dave yes its in cake1.3 - Ajay Patel
And your AppController file is in the app/ directory, not the app/controllers directory, yes? - Dave

3 Answers

4
votes

1.Solution: All what you need to do is to add this line in your UsersController:

public $components = array('Auth');

Or

2.Soluton: In UsersController:

App::uses('AppController', 'Controller');

class UsersController extends AppController {

}

and then in AppController:

public $components = array('Auth');
2
votes

There are two common causes for this type of error:

The App controller file is not being loaded

If the AppController class doesn't exist - Cake will use a fallback for it taken from the core - it's just an empty class. For the error in the question to occur - the Auth component hasn't been loaded, the most likely reason for that is that the file app/app_controller.php either doesn't exist or a different AppController class file was loaded before looking for it.

This can be confirmed using get_included_files, e.g.:

class UsersController extends AppController {

    public function beforeFilter() {
        if (!isset($$this->Auth)) {
            debug(get_included_files());
            die;
        }

Look for which app_controller.php file has been loaded - if it's not the file containing the class in the question, that's the problem.

Overridden constructor, not calling parent

Good children always call their parents :)

If the constructor (or any method) is overridden and does not call the parent function, then e.g. the components property won't get merged/set correctly or component classes won't be initialised.

If the right app_controller.php file is being loaded focus on the methods defined within the controller classes and check they are calling parent::methodname (for the users controller, and the app controller). Specifically verify that the Controller::__construct is being called since that's where most of the class initialization logic is in 1.3.

0
votes

The error sounds like the loading of the Auth-component failed. Have you tried removing the settings from the $components array and see if the error persists?