List all controllers / actions in Cakephp 3

How do I list all the controllers / actions on my site? Configure :: listObjects ('model') no longer exists. I am trying to write a function to create / add to an ACO in my ACL setup. Thanks.

+6
source share
3 answers

So here is what I did. In my resource controller:

Enable Class / Reflection Library

use ReflectionClass; use ReflectionMethod; 

To get the controllers:

 public function getControllers() { $files = scandir('../src/Controller/'); $results = []; $ignoreList = [ '.', '..', 'Component', 'AppController.php', ]; foreach($files as $file){ if(!in_array($file, $ignoreList)) { $controller = explode('.', $file)[0]; array_push($results, str_replace('Controller', '', $controller)); } } return $results; } 

And now for the actions:

 public function getActions($controllerName) { $className = 'App\\Controller\\'.$controllerName.'Controller'; $class = new ReflectionClass($className); $actions = $class->getMethods(ReflectionMethod::IS_PUBLIC); $results = [$controllerName => []]; $ignoreList = ['beforeFilter', 'afterFilter', 'initialize']; foreach($actions as $action){ if($action->class == $className && !in_array($action->name, $ignoreList)){ array_push($results[$controllerName], $action->name); } } return $results; } 

Finally, to tie them both together:

 public function getResources(){ $controllers = $this->getControllers(); $resources = []; foreach($controllers as $controller){ $actions = $this->getActions($controller); array_push($resources, $actions); } return $resources; } 

I hope this helps some people.

+9
source

It doesn't seem like anything that looks like this is still available in Cake3, and this is still necessary due to the namespaces that I think.

So you can try to do this:

  • Read all controllers from the application level controller folder
  • Read all the plugin controller folders (get the plugin folder via Plugin :: path () )
  • Iterate over the controllers you collected in the previous steps (you need to use App :: uses ())
  • Use reflection to get public methods from each controller.
+1
source

I am using CakePHP 3.x and had problems with the "getActions" function

The correct syntax for "ReflectionClass" and "ReflectionMethod":

  public function getActions($controllerName) { $className = 'App\\Controller\\'.$controllerName.'Controller'; $class = new \ReflectionClass($className); $actions = $class->getMethods(\ReflectionMethod::IS_PUBLIC); $results = [$controllerName => []]; $ignoreList = ['beforeFilter', 'afterFilter', 'initialize']; foreach($actions as $action){ if($action->class == $className && !in_array($action->name, $ignoreList)){ array_push($results[$controllerName], $action->name); } } return $results; } 

Warning for "\" before ReflectionClass and ReflectionMethod.

0
source

Source: https://habr.com/ru/post/975428/


All Articles