zacksleo / yii2-plugin
yii2 插件模块
2.1.6
2018-01-16 03:18 UTC
Requires
This package is auto-updated.
Last update: 2024-09-21 20:25:55 UTC
README
特性
- 此模块提供了一种插件模式(即插即用)解决方案。
- 无需编辑任何文件来配置插件,可以在管理员控制面板中安装、卸载、启用和禁用。
- 插件不会修改项目文件,可以安全卸载。
- 可扩展。可以为任何视图添加钩子。
- 封装 Yii 函数,方便非 Yii 开发者创建插件
模块使用
安装
composer install --prefer-dist zacksleo/yii2-plugin
将以下数组添加到项目配置中(如果有多个条目,请将它们添加到其中)
'components' =>[
'plugin' => [
'class' => 'zacksleo\yii2\plugin\components\HookRender'
],
],
'modules' => [
'plugin' => [
'class' => 'zacksleo\yii2\plugin\Module',
'layout' => 'layout',
'layoutPath' => '@vendor/zacksleo/yii2-backend/src/views/layouts', #布局
'pluginRoot' => '@vendor/moguyun-plugins/', ##放置插件的namespace目录
'pluginNamespace' => '@moguyun/plugins', ##放置插件的namespace
],
]
创建表
yii migrate/up --migrationPath=@zacksleo/yii2/plugin/migrations
链接到插件控制面板
CP 地址是
$this->createUrl(['/plugin/plugin-manage/index']);
在视图中添加钩子
#just add this to the position you want to be hooked
Yii::$app->plugin->render('Hook_Name'); # Name the Hook Position and told it to your plugin developers.
插件开发
创建插件
要创建插件,您需要继承 Plugin 类。
类名和类文件名应以单词 Plugin 结尾。例如,文件 ExamplePlugin.php
class ExamplePlugin extends Plugin {
//codes here
}
单词 Example(不带单词 Plugin)是插件的 标识(区分大小写)。### 实现插件要实现插件并使其工作,您应该继承这些方法和初始化一些属性。
class ExamplePlugin extends Plugin {
public function init() { #initialize,config plugin's info, required
// set plugin's info
$this->identify = 'Example'; #required, the Unique id for this plugin.
$this->name = 'Example Plugin'; #required, plugin's name for display.
$this->version = '1.0';
$this->description = 'description here';
$this->copyright = '© Robin <Robin@email.com>';
$this->website = 'http://example.com';
$this->icon = 'icon.png'; #max to 72*72, if not set a default icon will display in the admin cp;
}
// return hooks array which this plugin want to hook, the value is the method's name
// for the hook.
public function hooks(){
return array(
//'Hook Position Name' => 'hook method';
'Hook_Index_Header' => 'header',
);
}
// method for hook
public function header(){
// some codes here
echo 'This will echo a sting at position Hook_Index_Header';
}
// If you want to display a page with an url instead of render as a widget,
// you need to write a method begin with the word "action", the word after
// "action" is the action's name.
// e.g.:
public function actionPage() {
echo "This action have a url like this (with url rewrite):";
# domain/plugin<Module>/plugin<controller name>/index<router action name>
# ?id=xx<plugin identify>&action=xxx<action name>
echo "You_Domain.com/plugin/plugin/index?id=example&action=page";
#You can create this url by call method 'createUrl'
echo $this->createUrl('page',array('param'=>'test'));
}
public function actionExample(){
# this action is named with plugin's identify,
# param $action could be empty or false value
echo $this->createUrl();
}
// If your plugin allow to set configs at the admin control panel
// You need to inherit this:
public function admincp() {
// You can put codes here.
// Like some inputs
$this->setSetting('key','value'); # write setting
echo $this->getSetting('key'); # read setting
}
// Here you can put some code at the Installation and Uninstallation
public function install() {
//codes here
$sql = "create `tbl_xxxxxx` ....."; # write sql with a table prefix
$this->query($sql,'tbl_'); # and pass it at method query
# as default, it is 'tbl_'
return true; #This method need to return true, or the installation will fail.
}
public function uninstall() {
// just like the method install.
return true; #This method need to return true, or the uninstallation will fail.
}
// Then, you created a simple plugin
// For advanced usage, see demos
}