Cake php AppController and inheritance

In many tutorials on Acl component in cakephp I have an instruction that adds the component either to AppController or all other controllers that inherit AppController ........

but the problem

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

      

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

what problem will be

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';
}

}
?>

      

for userController

<?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 out line 4 it gives b error on termination it works fine in advance thanks

any help would be appreciated.

+2


a source to share


3 answers


I think there might be something wrong in your code. If you add a line to /app/cake/libs/controller/app_controller.php

, every child class should be able to use components. Take a look about the app_controller in the cookbook :

CakePHP bundles the following variables from AppController into Application Controllers: $components,$helpers,$uses

EDit @deceze



you can write your own custom base controller in /app/yourown_app_controller.php

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

      

then use it with requirment like require_once(APP."yourown_app_controller.php");

in child contrller file.

0


a source


I managed to pull it off. I had a couple of controllers that shared some of the methods, but other controllers in the application didn't (and couldn't) use those methods because some data had to be present in the model. Moreover, the controller containing the generic methods should not be called in the URL, which is an additional call.

  • Create a controller in the controllers folder named shared_controller.php. Define generic methods in it and make sure it extends AppController:

    class SharedController extends AppController {
    
          

  • My subcontrollers were extending AppController. Modify this to extend SharedController. I couldn't get a generic controller to load automatically, so I added App :: import above:

    App::import('Controller', 'Shared');
    class SubController extends SharedController {
    
          

  • Components and helpers must be merged into their parent variables manually; otherwise the child classes will overwrite them. Define a __construct method in each subcontroller and call array_merge for components, helpers, and everything else. Calling the parent constructor at the end. Note: this cannot be done in beforeFilter

    function __construct(){
        $this->components = array_merge($this->components,array('Mycomponent'));
        parent::__construct();
    }
    
          

  • If you call http://example.com/shared/method , you will probably get an error that you are missing a database table or a view is missing. I don't need any of the methods from Shared to call directly, so I defined a route to simply redirect to the home page:

    Router::connect('/shared/*', array('controller' => 'pages', 'action' => 'display', 'home'));
    
          



Hope this helps someone!

+5


a source


If anyone is wondering why they are being replaced $components

and $helpers

instead of merging after inheritance changes, you need to define protected $_mergeParent = 'YourParentClass';

. The default is "AppController".

Source: CakePHP 2.4 Source Code

+4


a source







All Articles