CakePHP:使用组件X列出所有控制器
长话短说:如何获取应用程序中使用特定组件的所有控制器CakePHP:使用组件X列出所有控制器,cakephp,components,cakephp-2.3,controllers,Cakephp,Components,Cakephp 2.3,Controllers,长话短说:如何获取应用程序中使用特定组件的所有控制器 public $components = array('MyPlugin.MyComponent'); 故事:我的应用程序中有多个控制器,其中一些使用一个组件 public $components = array('MyPlugin.MyComponent'); 在组件中或其他地方,有没有办法找出哪些控制器包含此组件,以便我可以列出它们?结果似乎很简单,但我不确定这是否是正确的方法,因为我觉得这似乎很粗糙: $controllers =
public $components = array('MyPlugin.MyComponent');
故事:我的应用程序中有多个控制器,其中一些使用一个组件
public $components = array('MyPlugin.MyComponent');
在组件中或其他地方,有没有办法找出哪些控制器包含此组件,以便我可以列出它们?结果似乎很简单,但我不确定这是否是正确的方法,因为我觉得这似乎很粗糙:
$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);
}
}
有更好的解决方案吗?我不这么认为,因为组件已加载到当前活动的控制器 当用户输入url时,它在服务器端由CakePHP解析为在控制器上,并且仅为该请求创建此url 为了尝试使用某些组件查找控制器,您可以搜索所有控制器,然后您可以尝试实例化每个控制器并读取其
$components
属性。这与您在回答中提出的基本相同,也有点粗糙(但可以采用基类参数)