Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all controllers/actions in Cakephp 3

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.

like image 467
MjGaiser Avatar asked Sep 17 '14 13:09

MjGaiser


People also ask

How can I get current controller name in cakephp 3?

Use $this->params['controller'] to get the current controller.

What is Controller in CakePHP?

The controller as the name indicates controls the application. It acts like a bridge between models and views. Controllers handle request data, makes sure that correct models are called and right response or view is rendered. Methods in the controllers' class are called actions.

What is loadModel cakephp?

The loadModel() function comes handy when you need to use a model table/collection that is not the controller's default one: // In a controller method. $ this->loadModel('Articles'); $recentArticles = $this->Articles->find('all', [ 'limit' => 5, 'order' => 'Articles.created DESC' ]);


2 Answers

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.

like image 110
MjGaiser Avatar answered Oct 14 '22 16:10

MjGaiser


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
like image 33
floriank Avatar answered Oct 14 '22 14:10

floriank