Search code examples
cakephpcomponentscakephp-2.3controllers

CakePHP: List all Controllers using Component X


Long story short: How do I get all controllers of my app that use a certain component.

The story: I have multiple controllers in my app, some of them using a component.

public $components = array('MyPlugin.MyComponent');

Is there a way, in the component or elsewhere, to find out which controllers include this component so I can list them?


Solution

  • Turns out it seems to be quite simple, but I am not sure if this is the correct way to do it, as it seems pretty hacky to me:

    $controllers = App::objects('controller');
    foreach($controllers as $controller) {
        App::import('Controller', str_replace('Controller', '', $controller));
        $properties = get_class_vars($controller);
    
        if(isset($properties['components']) && (isset($properties['components']['MyPlugin.MyComponent']) || in_array('MyPlugin.MyComponent', $properties['components']))) {
            $this->editable[] = str_replace('Controller', '', $controller);
        }
    }
    

    Any better solution out there?