企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 5. 插件事件 ## 事件说明 事件的定义写在`插件名称类`文件里面,方法命名方式以Hook结尾,字母驼峰式命名。 ## 插件能使用的事件 > 2.1.220504版本起插件事件不在使用下划线调用,但依然支持旧版的下划线方式 事件名称插件名称类使用方式描述参数说明addons\_init 2.1.220504版本起:addonsInitaddonsInitHook插件初始化,这个事件能影响到前台、后台,优先级最高-addonMiddlewareaddonMiddlewareHook插件全局中间件,只要访问任意插件前台时触发请求的Request对象addonsBeginaddonsBeginHook插件路由调度前事件,访问插件控制器时触发请求的Request对象addonModuleInitaddonModuleInitHook插件控制器初始化前事件,访问插件控制器时触发请求的Request对象addonsActionBeginaddonsActionBeginHook插件控制器操作方法执行前事件,访问插件控制器操作方法时触发传入控制器对象与将要操作的方法indexRouteindexRouteHook访问前台时,在路由初始化时触发,无adminRouteadminRouteHook访问后台时,在路由初始化时触发无apiRouteapiRouteHook访问api时时,在路由初始化时触发无configInitconfigInitHook访问前台或后台时,站点配置初始化后触发(前后台的基础类)站点配置adminLoginSuccessadminLoginSuccessHook后台登录成功后标签位登录信息indexHeadindexHeadHook前台模板头部标签位无indexFooterindexFooterHook前台模板底部标签位无themeChangethemeChangeHook模板主题切换事件传入index\_theme,或admin\_themeuploadAfteruploadAfterHook文件上传后触发传入文件信息数组uploadDeluploadDelHook文件删除后触发传入删除的模型对象addonConfig\_save插件保存配置时触发(已废弃)提交的配置表单插件标识ConfigSave插件或模板配置保存时触发(2.1.220220新增)提交的配置表单,例如你的插件叫:demo, 事件定义为:demoConfigSaveHookapiAuthMiddlewareapiAuthMiddlewareHookAPI权限中间件请求Request对象apiLoginMiddlewareapiLoginMiddlewareHookAPI登录中间件请求Request对象HttpRunHttpRunHook应用开始标签位(TP内置)无HttpEndHttpEndHook应用结束标签位(TP内置)当前响应对象实例RouteLoadedRouteLoadedHook路由加载完成(TP内置)无LogWriteLogWriteHook日志write方法标签位(TP内置)当前写入的日志信息## 使用方式 例如插件名称叫`demo`,在`Demo`插件名称类里面,若要使用`indexHead`前台模板头部事件。这里首先需要模板有放置该标签事件,可前往默认模板找到head.html文件,即可看到`{:hook("indexHead")}`,当访问前台页面执行到`{:hook("indexHead")}`时就会调用下面的`indexHeadHook`方法。 完整写法如下 ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php declare <span class="token1">(</span>strict_types<span class="token">=</span><span class="token3">1</span><span class="token1">)</span><span class="token1">;</span> namespace addons\demo<span class="token1">;</span> use think\Addons<span class="token1">;</span> class <span class="token6">Demo</span> extends <span class="token6">Addons</span> <span class="token1">{</span> public <span class="token2">function</span> <span class="token6">install</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span> <span class="token1">}</span> public <span class="token2">function</span> <span class="token6">uninstall</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span> <span class="token1">}</span> <span class="token5">// 前台模板执行到<head></head>时触发,通常用于加载JS、CSS达到特定的需求</span> public <span class="token2">function</span> <span class="token6">indexHeadHook</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token5">// 加载js</span> <span class="token2">return</span> <span class="token4">"<script src='/static/main.js'></script>"</span><span class="token1">;</span> <span class="token5">// 或直接渲染视图,可参考插件视图</span> <span class="token2">return</span> $this<span class="token">-</span><span class="token">></span><span class="token6">fetch</span><span class="token1">(</span><span class="token4">'index/index'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ## 扩展 当我们在写业务逻辑时,需要自己定义一些事件标签位来给其他插件实现功能扩展,那么我们可以参考下面来实现。 **1. hook方法** 系统有提供`hook`方法来触发事件,方法说明: ``` <pre class="calibre16">``` <span class="token5">/** * $event 事件名称 * $params 传入参数 * $once = true 表示返回一个结果,$once = false 表示返回全部结果 * $original = true 表示 返回TP trigger原始数据,false - 返回字符串,如果原样返回数据是数组即会转换成字符串 */</span> <span class="token6">hook</span><span class="token1">(</span>$event<span class="token1">,</span> $params<span class="token1">,</span> $once<span class="token1">,</span> $original<span class="token1">)</span> ``` ``` **2. 触发事件** 例如定义触发`showTest`事件。 ``` <pre class="calibre16">``` <span class="token5">// 控制器触发,传入数组</span> <span class="token6">hook</span><span class="token1">(</span><span class="token4">'showTest'</span><span class="token1">,</span> <span class="token1">[</span><span class="token4">'a'</span><span class="token">=</span><span class="token">></span><span class="token4">'测试'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">// 模板触发,传入数组</span> <span class="token1">{</span><span class="token1">:</span><span class="token6">hook</span><span class="token1">(</span><span class="token4">"showTest"</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'a'</span><span class="token">=</span><span class="token">></span><span class="token4">'测试'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">}</span> <span class="token5">// 接收返回值:输出原始数据。第四个参数给true即可</span> $arr <span class="token">=</span> <span class="token6">hook</span><span class="token1">(</span><span class="token4">"showTest"</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'a'</span><span class="token">=</span><span class="token">></span><span class="token4">'测试'</span><span class="token1">]</span><span class="token1">,</span><span class="token3">true</span><span class="token1">,</span><span class="token3">true</span><span class="token1">)</span><span class="token1">;</span> ``` ``` > 注意:当有多个插件定义showTest事件时,返回值是数组的,并不建议这样来使用事件 上面定义了`showTest`触发事件方法,那么在其他插件就可以定义`showTest`事件来实现扩展,如下 ``` <pre class="calibre20">``` <span class="token"><</span><span class="token">?</span>php declare <span class="token1">(</span>strict_types<span class="token">=</span><span class="token3">1</span><span class="token1">)</span><span class="token1">;</span> namespace addons\demo<span class="token1">;</span> use think\Addons<span class="token1">;</span> class <span class="token6">Demo</span> extends <span class="token6">Addons</span> <span class="token1">{</span> public <span class="token2">function</span> <span class="token6">install</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span> <span class="token1">}</span> public <span class="token2">function</span> <span class="token6">uninstall</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token3">true</span><span class="token1">;</span> <span class="token1">}</span> <span class="token5">// 对应showTest</span> public <span class="token2">function</span> <span class="token6">showTestHook</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> echo <span class="token4">"测试"</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ```