Back to blog

List all controllers in Cakephp 2

I needed to list all the Controllers of an Cakephp 2 application. I searched the webz and found an old article that got me started. The original article with code is here:

Here is the (rewritten) code for a CakePHP 2 Component:


//File: application/Controller/Component/CtrlComponent.php
// Component rewritten, original from :
class CtrlComponent extends Component {

     * Return an array of user Controllers and their methods.
     * The function will exclude ApplicationController methods
     * @return array
    public function get() {

        $aCtrlClasses = App::objects('controller');

        foreach ($aCtrlClasses as $controller) {
            if ($controller != 'AppController') {
                // Load the controller
                App::import('Controller', str_replace('Controller', '', $controller));

                // Load its methods / actions
                $aMethods = get_class_methods($controller);

                foreach ($aMethods as $idx => $method) {

                    if ($method{0} == '_') {

                // Load the ApplicationController (if there is one)
                App::import('Controller', 'AppController');
                $parentActions = get_class_methods('AppController');

                $controllers[$controller] = array_diff($aMethods, $parentActions);

        return $controllers;


Place this in application\Controller\Component\CtrlComponent.php and use this in any Controller you want:

public $components = array('Ctrl');

public function list() {
$aControllers = $this->Ctrl->get();

Go to your controller action (for example: /mycontroller/list) and it will show you all the controllers with their methods.

Labels: cakephp php

16 Responses

  1. Don’t work to me! :'(

    Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\test\app\View\Layouts\default.ctp on line 34

    this function only work in controller!? :/

    • Peter Prins on January 3, 2012

      Hi Wallace,

      Yes you’re right, Components are ‘Controller addons’. You use Components when you have code that you could use in every Controller.

      “Components provide reusable bits of controller logic that can be composed into a controller.”

      Good luck!

  2. Sulian on January 24, 2012

    foreach ($aMethods as $method => $idx) {

    must be

    foreach ($aMethods as $idx => $method) {

    • Peter Prins on January 26, 2012

      Thanks Sulian,

      You’re right, I’ve changed it.

  3. Annamalai on March 27, 2012

    Thanks Lot… Good work :)

    • Peter Prins on March 27, 2012

      No thanks, for example, I’ve used it to secure controllers in an ACL module.

  4. Rafael on June 15, 2012

    Thanks dude! Worked perfectly :D

  5. How I list this array in the View?


    class ClientsController …{
    public $components = array(‘Ctrl’);

    public function List … {
    $aControllers = $this->Ctrl->get();

    in the View:


    • Peter Prins on October 5, 2012

      In the controller assign the array to the view.
      For example:

      < ?php
      // The var $controllers will be available in your View
      $this->set('controllers', $aControllers);

      You can loop through the array in your View.

      < ?php
      // Your View
      foreach($controllers as $key => $controller) {
         // Do your thing
    • thanks very much!!! =D

  6. Thanks for the above information by giving a brief description and letting the customer know about the all Controllers in CakePHP2.

  7. Nice one, I adapted this and it works perfectly for what I needed!

    • Peter Prins on March 8, 2013

      Thanks Adam, can you confirm it works with CakePHP 2.3.x ? Or what version are you using?

  8. aviemet on November 10, 2013

    I just wanted to point out that you could save loading the AppController many times if the App::import(…) statement and $parentActions variable were brought outside of the loop. It’s the same information each time through and only needs to be done once.

  9. Adrian on September 6, 2014

    Two Years after, i need same.
    My solution based in this code is teh next:

    $metodosAppController = get_class_methods(‘AppController’);

    $controladores = App::objects(‘controller’);


    foreach ($controladores as $controlador){

    App::import(‘Controller’, str_replace(‘Controller’, ”, $controlador));
    $metodosTemp = get_class_methods($controlador);
    $metodos[str_replace(‘Controller’, ”, $controlador)] = array_diff($metodosTemp, $metodosAppController);


    Sorry, my english is very bad.

    • Adrian on September 6, 2014

      This function hace a one BIG problem, don’t list a protected and private methods
      If any one know how, pls post it!

Drop a note