debuggable

 
Contact Us
 

Quick dessert: List all controllers of a CakePHP application

Posted on 12/7/07 by Felix Geisendörfer

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();
    $controllers = array();
    foreach($Configure->controllerPaths as $path) {
        $controllers = am($controllers, array_map(array(&$this, '__controllerize'), listClasses($path)));
    }
   
    return array_unique($controllers);
}

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();
    $controllers = array();
    foreach($Configure->controllerPaths as $path) {
        $Folder->cd($path);
        $controllerFiles = $Folder->find('.+_controller\.php$');
        $controllers = am($controllers, array_map(array(&$this, '__controllerize'), $controllerFiles));
    }
   
    return array_unique($controllers);
}

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

 
&nsbp;

You can skip to the end and add a comment.

Henrique said 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 said on Jul 12, 2007:

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

...

majna  said on Jul 13, 2007:

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

Felix Geisendörfer said 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 said on Jul 19, 2007:

Singleton sux :]

Felix Geisendörfer said 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  said 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  said on Dec 06, 2007:

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

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

Marko  said 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');

}

Ivan said on Jan 15, 2009:

Okay, since uses() is depreciated, how can I import the "Folder" class?

Tim Koschützki said on Jan 15, 2009:

App::import('Core', 'Folder');

Tried that one yet?

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.