Quicktips: Finding all the actions in your controller

Published: on 11/9/08 | Comments (0)

Hi All,

Thought I'd share a quick tip today for how to automatically detect all the action names within a given controller.

This follows on from my last quicktip, describing how to detect all the controllers in your application and is usefull from witin navigation code or admin back ends.

$actions = get_class_methods($this);

This returns an array where each element is the name of a method within the current controller.

Filtering your results

You can make this more usefull by filtering the actions, for example to remove all the inherited actions from app_controller use:

$ignore = get_class_methods('Controller');
foreach($actions as $key=>$action){
            if(in_array($action, $ignore)){
                unset($actions[$key]);
                continue;
            }
}

and this can be extended further to remove any private or protected methods with:

$ignore = get_class_methods('Controller');
foreach($actions as $key=>$action){
            if(in_array($action, $ignore)){
                unset($actions[$key]);
                continue;
            }
            if(strpos($action, '_', 0) === 0){
                unset($actions[$key]);
                continue;
            }
}

That's it for today, hope some of you find this helpfull, see you all soon.


Comments

Have Your Say

Comments are now closed for this article


About Studio Canaria

Studio Canaria is the web site of freelance web developer, Peter Butler. Articles on this site relate to designing, developing and marketing modern web applications.

Recent Comments

Dave on CakePHP Auth Component - Users, Groups & Permissions Revisited
I dont understand how you define permissions for each contoller to User. Do I have to manually...
SI on CakePHP Auth Component - Users, Groups & Permissions Revisited
Hi Peter,

I am having little issue logging into the application. I have 2 user groups both having...
Alig on CakePHP Auth Component - Users, Groups & Permissions Revisited
Thank you very much for this tutorial. After fighting for hours to get the cakephp ACL working,...
Benny on CakePHP Auth Component - Users, Groups & Permissions Revisited
thank you for your solution it's perfect , you are genius , thank you this is the best ( beside...
Gordon on CakePHP Auth Component - Users, Groups & Permissions Revisited
I'm having a similar problem to luis. I followed everything as instructed here up to the point...