Dessert #10 - Default Models, Components & Helpers
Posted by Felix Geisendörfer, on Sep 17, 2006 - in PHP & CakePHP » Core & Hacking
Deprecated post
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).
Important: It appears that this method is and has not been needed at all at least for a while. This was one of the first little AppController hacks I used when I started CakePHP in november/december last year and apparently at some point there was an improvement in the Controller class making it unnessarry. I wish those *little* features would be posted somewhere regulary, similar to nate's postings about 1.2.
We all know the problem, there are always Models, Components or Helpers that we need for about 99% of all our actions/views and therefor would fit beautifully in our AppController. The only problem is, as soon as we define them via "var $uses = array('...');" (etc.), we cannot easily add more items in our derived controllers without having to repeat the entire list.
Today I actually felt like writing little piece of code to finally DRY the probem. The basic idea is to not add the items to the AppController by using the "var" command, but rather by overwriting it's __construct() function and adding them there. I already used this technic before, but this time I added a little algorithm to it that would automate the array_merging:
-
function __construct()
-
{
-
/**
-
* Default Models, Components and Helpers
-
*/
-
$default['components'] = array('RequestHandler', 'Themes', 'Header', 'MenuManager', 'SimpleAcl', 'SimpleAuth');
-
$default['helpers'] = array('Html', 'Lugsteinhof', 'Lists', 'Accessibility', 'Javascript', 'Form', 'Time');
-
-
foreach ($default as $type => $items)
-
{
-
-
}
-
-
parent::__construct();
-
}
Let me know if you find this approach useful or what kind of technic you are using to solve this problem.
--Felix Geisendörfer aka the_undefined