eom / plugin-cakephp-loadsubcomponents
用于在子文件夹中加载子组件的 CakePHP 2.x 插件。
v0.2.0
2020-10-09 23:29 UTC
Requires
- php: >=5.3.0
- cakephp/cakephp: >=2.10
- composer/installers: *
README
使用此组件,您可以在 Controller/Component/.../ 目录中创建子目录,然后无需受 CakePHP 2.x 对此问题的原生限制而加载它们。
1. 使用 composer 安装
编辑您的 composer.json 文件并做出以下更改。 https://packagist.org.cn/packages/eom/plugin-cakephp-loadsubcomponents
"extra": {
"installer-paths": {
"app/Plugin/EOM/{$name}/": ["vendor:eom","type:cakephp-plugin"],
"app/Plugin/{$name}/": ["type:cakephp-plugin"]
}
}
这可能会根据您的 CakePHP 2.x 配置以及您为 composer 配置的 vendors 目录而有所不同。在我的情况下,采用以下结构,如果您有不同的结构,请在 composer.json 文件中调整路径:"app/Plugin/EOM/{$name}/": ["vendor:eom","type:cakephp-plugin"]
路径树
|-> app | |-> Plugin | |-> ... | |-> EOM | |-> LoadSubComponents | |-> ... |-> vendors | |-> bin | |-> composer | |-> ... | |-> autoload.php
2. 配置 bootstrap.php 以加载插件
随后,在 app/Config/bootstrap.php 文件的末尾或与其他插件一起添加以下行:
// Load Plugins ... CakePlugin::load('EOM/LoadSubComponents', array('bootstrap' => false, 'routes' => false));
3. 在 AppController 中全局配置组件
如何配置组件以供整个系统使用,并在子目录中加载其他组件
/** * Application Controller * @property LoadSubComponentsComponent $LoadSubComponents */ class AppController extends Controller { public $components = ['EOM/LoadSubComponents.LoadSubComponents']; }
4. 在任何继承自 AppController 的控制器中使用它们
以下是如何在 beforeFilter 或操作中使用 LoadSubComponents 的示例。
/** * MiDemo Controller * @property MiFunctionDemo1Component $MiFunctionDemo1 * @property MiFunctionDemo2Component $MiFunctionDemo2 * @property MiFunctionDemo3Component $MiFunctionDemo3 */ class MiDemoController extends AppController { public function beforeFilter() { parent::beforeFilter(); // Load path Controller/Component/SubDirectorios/TestDemoComponent.php $this->TestDemo = $this->LoadSubComponents->Load('SubDirectorios/TestDemo'); // Load path Controller/Component/SubDirectorioX/MiFunctionDemo1Component.php $this->MiFunctionDemo1 = $this->LoadSubComponents->load('SubDirectorioX/MiFunctionDemo1'); $this->MiFunctionDemo3 = $this->LoadSubComponents->load('SubDirectorio/SubX/MiFunctionDemo3'); // Demo1 de como utilizarla $this->MiFunctionDemo1->MiAccion1('Demo'); $this->MiFunctionDemo1->MiAccion2('Demo'); } public function index(){ // TestDemo de como utilizarla echo $this->TestDemo->hello('Hi EOM !!!'); // Load path Controller/Component/SubDirectorioX/MiFunctionDemo2Component.php $this->MiFunctionDemo2 = $this->LoadSubComponents->load('SubDirectorioX/MiFunctionDemo2'); // Demo2 de como utilizarla $this->MiFunctionDemo2->MiAccion1('DemoIndexBla'); // Reutilizar Demo1 load en el __construct $this->MiFunctionDemo1->MiAccion101('DemoIndexBlaBla..'); // Utilizar Demo3 load en el __construct $this->MiFunctionDemo3->MiAccionDemoBla('DemoIndexBlaBla..'); } // ... }