# think-plugin **Repository Path**: sycms/think-plugin ## Basic Information - **Project Name**: think-plugin - **Description**: No description available - **Primary Language**: PHP - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-04-26 - **Last Updated**: 2023-04-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # think-plugins The ThinkPHP 6 plugins Package ## 安装 > composer require uknow/think-plugin ## 配置 ### 生成配置 系统安装后会自动在 config 目录中生成 plugins.php 的配置文件, 如果系统未生成可在命令行执行 ```php php think plugins:config ``` 快速生成配置文件 ### 公共配置 ```php 'plugins' => [ // 是否自动读取取插件钩子配置信息(默认是开启) 'autoload' => true, // 当关闭自动获取配置时需要手动配置hooks信息 'hooks' => [ // 可以定义多个钩子 'testhook'=>'test' // 键为钩子名称,用于在业务中自定义钩子处理,值为实现该钩子的插件, // 多个插件可以用数组也可以用逗号分割 ], 'route' => [], 'service' => [], ]; ``` 或者在\config目录中新建`plugins.php`,内容为: ```php false, // 当关闭自动获取配置时需要手动配置hooks信息 'hooks' => [ // 可以定义多个钩子 'testhook'=>'test' // 键为钩子名称,用于在业务中自定义钩子处理,值为实现该钩子的插件, // 多个插件可以用数组也可以用逗号分割 ], 'route' => [], 'service' => [], ]; ``` ## 创建插件 > 创建的插件可以在view视图中使用,也可以在php业务中使用 安装完成后访问系统时会在项目根目录生成名为`plugins`的目录,在该目录中创建需要的插件。 下面写一个例子: ### 创建test插件 > 在plugins目录中创建test目录 ### 创建钩子实现类 > 在test目录中创建 Plugin.php 类文件。注意:类文件首字母需大写 ```php 'test', // 插件标识 'title' => '插件测试', // 插件名称 'description' => 'thinkph6插件测试', // 插件简介 'status' => 0, // 状态 'author' => 'byron sampson', 'version' => '0.1' ]; /** * 插件安装方法 * @return bool */ public function install() { return true; } /** * 插件卸载方法 * @return bool */ public function uninstall() { return true; } /** * 实现的testhook钩子方法 * @return mixed */ public function testhook($param) { // 调用钩子时候的参数信息 print_r($param); // 当前插件的配置信息,配置信息存在当前目录的config.php文件中,见下方 print_r($this->getConfig()); // 可以返回模板,模板文件默认读取的为插件目录中的文件。模板名不能为空! return $this->fetch('info'); } } ``` ### 创建插件配置文件 > 在test目录中创建config.php类文件,插件配置文件可以省略。 ```php [ 'title' => '是否显示:', 'type' => 'radio', 'options' => [ '1' => '显示', '0' => '不显示' ], 'value' => '1' ] ]; ``` ### 创建钩子模板文件 > 在test->view目录中创建info.html模板文件,钩子在使用fetch方法时对应的模板文件。 ```html