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