List all controllers/actions in Cakephp 3
Asked Answered
T

3

12

How do I list all the controllers/actions on my site? Configure::listObjects('model') doesnt seem to exist anymore. I am trying to write a function to generate/add to the ACO's in my ACL setup. Thanks.

Towbin answered 17/9, 2014 at 13:55 Comment(0)
T
19

So here is what I did. In my Resource Controller:

Include the reflection class/method libraries

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 boths together:

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

I hope that helps some people.

Towbin answered 28/9, 2014 at 3:52 Comment(0)
D
1

It doesn't look like anything similar to this is still available in Cake3, nor is it still needed because of the namespaces I think.

So in short you can try to do this:

  • Read all controllers from the app level controller folder
  • Read all plugin controller folders (Get the plugin folder via Plugin::path())
  • Iterate over the controllers you've collected in the previous steps (You'll need to use App::uses())
  • Use reflections to get the public methods from each controller
Dex answered 17/9, 2014 at 23:11 Comment(0)
H
0

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

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

    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.

Hanhana answered 3/7, 2017 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.