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?

有帮助吗?

解决方案 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?

其他提示

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).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top