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.
