List all controllers/actions in Cakephp 3

2019-02-05 02:16发布

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.

3条回答
三岁会撩人
2楼-- · 2019-02-05 02:56

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-05 03:10

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
查看更多
走好不送
4楼-- · 2019-02-05 03:17

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.

查看更多
登录 后发表回答