我想用Cakephp3在插件中创建一个插件。我为Cakephp2找到了一个解决方案,但它在Cakephp3中似乎不起作用:
Is it possible to create a plugin inside a plugin with CakePHP?
我如何在Cakephp3中做到这一点?
发布于 2017-03-05 03:14:04
假设您的问题是将插件加载到CakePHP 3.x应用程序中,而不是创建插件:-)
备注:这个答案假设您使用composer安装了Cake。
举个例子,假设我们想创建一个插件,Themes,这个插件将包含其他插件,Blue和Red。
按照惯例,主题插件应该包含在您的_app/ plugins /Themes中,Blue和Red插件可以分别包含在您的_app/plugins/Themes/plugins/Blue和您的_app/plugins//插件中。
在您的app/config/bootstrap.php中,添加以下内容:
Plugin::load('Themes', ['bootstrap' => true]);(有关插件配置的信息,请参见https://book.cakephp.org/3.0/en/plugins.html#plugin-configuration )
上面的代码告诉Cake加载主题插件,并查找并加载插件的引导文件。
如果您还没有这样做,请在您的_app/plugins/ Themes /config/bootstrap.php中创建主题插件的引导文件,并使其看起来类似于:
<?php
use Cake\Core\Plugin;
// load the red and blue child plugins
Plugin::load('Themes/plugins/Red');
Plugin::load('Themes/plugins/Blue');重要的:由于您试图手动编写插件,而不是通过composer安装,所以您需要修改_app/Composer.json以包含以下内容:
"autoload": {
"psr-4": {
"App\\": "src",
"Red\\": "./plugins/Themes/plugins/Red/src",
"Blue\\": "./plugins/Themes/plugins/Blue/src"
}
}(有关自动插件类的更多信息,请参见https://book.cakephp.org/3.0/en/plugins.html#autoloading-plugin-classes )。
现在,在您的应用程序/运行中:
php composer.phar dumpautoload(或等效命令,取决于在计算机上安装composer的方式)
这告诉编写器刷新它的自动缓存。如果你要检查你的应用程序/供应商/cakephp-plugins.php,你应该会发现一个指向主题插件文件夹的路径已经被添加到一个已经存在的插件路径列表中。
现在,从应用程序的主控制器中,您应该可以拥有如下内容:
public function initialize()
{
// load (supposedly-existing) components from the "Red" or "Blue" themes
// load GradientComponent of the "Red" theme
$this->loadComponent('Red.Gradient');
// load ColorComponent of the "Blue" theme
$this->loadComponent('Blue.Color');
// use what you asked for...
$this->Color->someMethod(['data']);
parent::initialize();
}此外,要使用视图文件(您希望主题插件提供:- ):
public function beforeRender(Event $event)
{
// use the "home" layout from the Red theme
$this->viewBuilder()->setLayout('Themes/plugins/Red.home');
parent::beforeRender($event);
}https://stackoverflow.com/questions/41552069
复制相似问题