Question

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?

Was it helpful?

Solution 2

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?

OTHER TIPS

I don't think so since components are loaded to currently active controller.

When user enters url it is resolved on server side by CakePHP to on of your controllers and only this one is created for this request.

In order to try find Controllers using some component you can search for all controllers by App::objects('Controller') then you can try to instantiate each Controller and read it's $components property. Which is pretty much the same which you proposed in you answer and is also a bit hacky (but can take take base class parameters IMO).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top