In CakePhp2 we could get the list of methods in a controller in the following way:
App::import('Controller', 'TagsController');
$classMethods = get_class_methods('TagsController');
But in CakePhp3 App::import is not working. Then what's the way to get the list of methods of that controller in CakePHP3?
Thanks
Please pay attention to this link. I think the App::import()
changes to App::classname()
https://book.cakephp.org/3.0/en/core-libraries/app.html
You can use this part.
// Names with \ in them will be returned unaltered.
App::classname('App\Cache\ComboCache');
// Returns App\Cache\ComboCache
get_class_methods('App\Cache\ComboCache')
You can get all method in a controller using php ReflectionMethod
class
use ReflectionMethod;
public function getActions($controllerName) {
$className = 'App\\Controller\\' . $controllerName . 'Controller';
$class = new ReflectionClass($className);
$actions = $class->getMethods(ReflectionMethod::IS_PUBLIC);
$controllerName = str_replace("\\", "/", $controllerName);
$results = [$controllerName => []];
$ignoreList = ['beforeFilter', 'afterFilter', 'initialize', 'beforeRender'];
foreach ($actions as $action) {
if ($action->class == $className
&& !in_array($action->name, $ignoreList)
) {
array_push($results[$controllerName], $action->name);
}
}
return $results;
}
If your want to get Users
controller method list. Then just call $this->getActions('Users')
Hope it will help you.