tags:

views:

334

answers:

1

hello folks........

In many tutorial of Acl component in cakephp i got instruction that add component either in AppController or in all the other controllers which inherits AppController.......

but problem is

var $components=array('Auth',Acl);

when i use the above line in AppConroller i cant use the Auth or Acl component... but when i use the same code in all the child classes it works fine........

what will be the problem

here is my appController

<?php
class AppController extends Controller {

     var $helpers = array('Html', 'Form', 'Session','CssMenu');
     var $components = array('Auth');

     function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->authorize = 'actions';
        $this->Auth->authError = "Sorry, you are lacking access.";
        $this->Auth->userModel = 'Login';
}

}
?>

code for usersController

<?php
class userssController extends AppController{
    var $name="Logins";
    //var $components = array('Auth');
    var $layout='login';

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('*');
        $this->Auth->loginRedirect = array('controller' => 'homes', 'action' => 'index');
    }
}
?>

when i comment the line 4 this will b error on uncomment it works fine thanks in advance

any help will be appreciated.

+1  A: 

I think there may be something wrong in your code.If you add the line in /app/cake/libs/controller/app_controller.php,every child class should be able to use the components.See about app_controller in cookbook:

CakePHP merges the following variables from the AppController to your application's controllers:$components,$helpers,$uses

EDit @deceze

you may write your own customized base controller in /app/yourown_app_controller.php

class YourOwnAppController extends Controller
{
       var $components = array("Auth");
}

then use it by a requirment like require_once(APP."yourown_app_controller.php"); in the child contrller file.

SpawnCxy
+1 for the general consensus that it should be available if put into AppController, -1 for suggesting to edit `/cake/libs/*`. Never touch the `/cake` directory unless there's a bug to fix. You can and should put the `app_controller.php` in your `/app` directory!
deceze
@deceze,that's right.Thanks for the note.
SpawnCxy
Sorry, your edit is wrong as well. You make a class called `AppController` in a file called `app_controller.php` in your `/app` directory. That's the way it's supposed to be done, Cake explicitly supports it this way. http://book.cakephp.org/view/829/The-App-Controller
deceze
@deceze,I'm afraid I cannot agree with you at this point.It's not necessary to make custom `appcontroller` always as `app_controller.php`,because people may not always need only one kind of base controller to extend.
SpawnCxy
Okay, technically, you *can* make custom base controllers, true. It's bad to forgo convention though, especially with no gain. If you have a situation where you need two different base controllers (...?) you could use this method, but that has nothing to do with this question.
deceze
@deceze,In my last project for recruiting ,we used some base controllers for different roles such as jobseeker,admin,companyusers..etc,I think it's better than just extend one `appcontroller`.:)
SpawnCxy
thanks guys thanks for this informative discussion.Now i understood were i am going wrong and corrected.
RSK
@SpawnCxy : can i have a base controller which extends AppController for UsersController???if possible where i need to place the base controller (i mean which directory)-----------------so my hierarchy will be AppController->BaseController->UsersController
RSK
@RSK,yes,that's possible.Place the base controller in `app/` and require it as my answer said.
SpawnCxy
@SpawnCxy : thanks alot
RSK
how can i use myown login function to authenticate users in cakePHPbecause my users table structure is different from the cakePHP structure.even i overridden the login function it still executing the login function in parent class
RSK