A simple component for CakePHP 2.x which returns a list of controllers and the corresponding action names.
Requires at least PHP 5.3.
Copy the file app/Controller/Component/ControllerListComponent.php to the Controller/Component folder of your application.
First, you have to add the component to the $components array of your controller(s):
public $components = array('ControllerList');Then you can use the component in your action(s) with: $this->ControllerList->getList(). You can also specify the controllers which should be excluded from the returned list: $this->ControllerList->getList(array('UsersController')). Please note that without this parameter, the PagesController is automatically excluded.
The structure of the returned array is like:
<?php
array(
'ExampleController' => array(
'name' => 'Example',
'displayName' => 'Example',
'actions' => array(
(int) 0 => 'index',
(int) 1 => 'show'
)
),
'VendorExampleController' => array(
'name' => 'VendorExample',
'displayName' => 'Vendor Example',
'actions' => array(
(int) 0 => 'index',
(int) 1 => 'callback',
(int) 2 => 'api'
)
)
)The component provides two configuration options.
The first option, includePlugins, determines whether controllers in plugins are listed. By default, those controllers are ignored.
public $components = array('ControllerList' => array('includePlugins' => true));The second option, parentControllers, allows you to specify parent controllers other than AppController.
public $components = array('ControllerList' => array('parentControllers' => array('ParentController')));- Added the configuration option
includePlugins. Thanks to Oscar!
- Added the configuration option
parentControllers. Thanks to leodisarli!
- Added parameter
$controllersToExcludeto thegetList()method - Changed the structure of the returned array by including the keys
name,displayNameandactions. Thanks to Charles A. Beasley!
- Adapted for CakePHP 2.x. Thanks to Paolo Iulita!
- Initial release
If you have questions or feedback, feel free to contact me via Twitter (@dhofstet) or by email ([email protected]).
The ControllerList component is licensed under the MIT license.