tags:

views:

848

answers:

4

Has anyone attempted this? Is it possible, and if so, what kind of problems will I run into if I try to accomplish it?

A: 

Why not try? At least, it's already being done with the Controller -> AppController -> MyController classes.

Adriano Varoli Piazza
A: 

Sure, you can easily subclass controllers in Cake.. and then you'd typically utilize their hook function such as 'beforeFilter', 'afterFilter', etc. to add runtime logic to your controllers.. I typically put at least 1 abstract controller between cakes controller and the final controller that you configure in the route.

DreamWerx
+1  A: 

I have put an additional layer between the AppController and some special controllers in an app.

The only problem you'll run into is the merging of the $helpers and $components class attributes. In CakePHP, overriding those variables in your controllers will not overwrite those set by the AppController, but it will merge them.

This is done by a special method named __mergeVars() in the Controller base class, and it unfortunately does it only for the default controller structure. Your additional layer will not be merged correctly, if you want $helpers and $controllers inheritance from AppController down to your controllers.

rscherer
+2  A: 

If you goal is to share logic between controllers:

  • add the logic to the AppController to share it with all the controllers in your app.

  • make a component and add that to $this->components for the controllers you want to share it.

Adding additional inheritance between controllers should only be concidered as a last resort, as you have to pay extra attention to how components and helpers are handled. E.g. you must manually merge $this->components & $this->helpers with the AppController and the controller you are inheriting from.

Ronny Vindenes