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:

php
  1. function listControllers() {
  2.     $Configure =& Configure::getInstance();
  3.     $controllers = array();
  4.     foreach($Configure->controllerPaths as $path) {
  5.         $controllers = am($controllers, array_map(array(&$this, '__controllerize'), listClasses($path)));
  6.     }
  7.    
  8.     return array_unique($controllers);
  9. }
  10.  
  11. function __controllerize($file) {
  12.     return Inflector::camelize(r('_controller.php', '', $file));
  13. }

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:

php
  1. function listControllers() {
  2.     uses('Folder');
  3.     $Folder =& new Folder();
  4.  
  5.     $Configure =& Configure::getInstance();
  6.     $controllers = array();
  7.     foreach($Configure->controllerPaths as $path) {
  8.         $Folder->cd($path);
  9.         $controllerFiles = $Folder->find('.+_controller\.php$');
  10.         $controllers = am($controllers, array_map(array(&$this, '__controllerize'), $controllerFiles));
  11.     }
  12.    
  13.     return array_unique($controllers);
  14. }
  15.  
  16. function __controllerize($file) {
  17.     return Inflector::camelize(r('_controller.php', '', $file));
  18. }

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

Print this Post | Digg This | Stumble It | Delicious

10 Comments

Henrique on Jul 12, 2007:

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.

PHPDeveloper.org on Jul 12, 2007:

Felix Geisendorfer's Blog: Quick dessert: List all controllers of a CakePHP application...

...

majna on Jul 13, 2007:

This works fine too:
$controllerList = listClasses(APP.'controllers'.DS);

Felix Geisendörfer on Jul 13, 2007:

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.

mod rewrite on Jul 19, 2007:

Singleton sux :]

Felix Geisendörfer on Jul 19, 2007:

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. [...]

neroz on Aug 06, 2007:

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.

Sliv on Dec 06, 2007:

Here's a modification that will get a list of Controllers *or* Models:

http://bin.cakephp.org/saved/25949

Marko on Mar 17, 2008:

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');
}

Add a comment