Quick dessert: List all controllers of a CakePHP application
Posted by Felix Geisendörfer, on Jul 12, 2007 - 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 ; ).
Hi folks,
being busy as usual I don't have much time for blogging about the 'really' cool stuff. However here is a quick little dessert showing you how to get an array with all controllers in your application.
Update:: Thx to majna for pointing this out: The cake core has a function called listClasses() which can be used to find the controller files more quickly. Here is a new version making use of it:
-
function listControllers() {
-
$Configure =& Configure::getInstance();
-
foreach($Configure->controllerPaths as $path) {
-
}
-
-
}
-
-
function __controllerize($file) {
-
return Inflector::camelize(r('_controller.php', '', $file));
-
}
I'll leave the old code around as well since even if it's less effective it still shows how one can use the Folder class:
-
function listControllers() {
-
uses('Folder');
-
$Folder =& new Folder();
-
-
$Configure =& Configure::getInstance();
-
foreach($Configure->controllerPaths as $path) {
-
$Folder->cd($path);
-
$controllerFiles = $Folder->find('.+_controller\.php$');
-
}
-
-
}
-
-
function __controllerize($file) {
-
return Inflector::camelize(r('_controller.php', '', $file));
-
}
It's something I've heard people asking for in IRC quite some times by now and since I just needed it again I thought I might as well publish it here. I hope somebody out there finds it useful.
-- Felix Geisendörfer aka the_undefined
10 Comments
Felix Geisendorfer's Blog: Quick dessert: List all controllers of a CakePHP application...
...
This works fine too:
$controllerList = listClasses(APP.'controllers'.DS);
majna: Not exactly, it just gives you an array with the file names. However that itself can be used to simplify my code. So I just updated the blog post with a new version. Thanks for pointing it out.
Singleton sux :]
mod rewrite: ?
[...] Jul 30th, 2007 by didip This tips is written by Felix Geisendörfer aka the_undefined. He is waaaaay involved with CakePHP community. [...]
With a little bit of modification I have accomplished something like that: http://bayimg.com/PAFEDaABF
It shows controller/action permission (SimpleAcl).
Here is incomplete code (php5 only due to of reflection usage): http://bin.cakephp.org/view/2120519177
It have some bugs.
Here's a modification that will get a list of Controllers *or* Models:
http://bin.cakephp.org/saved/25949
I found out that in Cake 1.2 listClass is deprecated and instead we should use listObjects. I needed just an array of unchanged controller names, so I used this
function getControllers() {
$Configure = &Configure::getInstance();
return $Configure->listObjects('controller');
}



THANKS MAN!
This, together with a helper for creating tabbed menus (kinda like your 'McGyver menu'), can become a automatic menu generator. Which is exactly I was looking for.