Question

I have a custom structure in my CakePHP app which goes like this:

class AppController extends Controller // default

class ExtendedAppController extends AppController

class ChildController extends ExtendedAppController

The components I declare in ExtendedAppController get erased when I declare components in a ChildController class. I guess I will have this same problem with helpers also. How do I merge the arrays to avoid this?

Was it helpful?

Solution

Cake merges the current controller's variables with only ONE parent class which by default is set in the variable $_mergeParent = 'AppController'; in the core Controller class.

You can override this variable in your ChildController by defining:

class ChildController extends ExtendedAppController {

    protected $_mergeParent = 'ExtendedAppController';

}

However, this will ignore all the helpers and components defined in AppController, so copy the components and helpers from your AppController to your ExtendedAppController. This should answer your question I guess as you will be able to use ExtendedAppController's components from your ChildController and other controllers extending AppController will use AppController's components.

It is the way the Controller::_mergeControllerVars() method is written in the core. This is precisely why the book says:

The HtmlHelper, FormHelper, and SessionHelper are available by default, as is the SessionComponent. But if you choose to define your own $helpers array in AppController, make sure to include HtmlHelper and FormHelper if you want them still available by default in your Controllers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top