多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 15. 插件中间件 ## 插件中间件 插件中间件用于访问插件前台、或前台、后台时对HTTP请求拦截或过滤。例如你可以使用中间件控制哪些方法需要登录才能访问。 > 注意:2.1.220220版本暂时不支持控制器中间件,访问插件前台可以使用的addonMiddleware事件,该事件监听所有访问插件前台的操作,详情前往插件事件了解。 ## 创建中间件 中间件放置于:addons/demo/middleware/ ![](https://img.kancloud.cn/52/f6/52f6f241e0b4d75a4bdc422d00fbc1d4_275x202.png) 示例代码: ``` <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\middleware<span class="token1">;</span> class <span class="token6">Login</span> <span class="token1">{</span> <span class="token5">// handle 为入口执行方法,必须。第一个参数是`Request`对象,第二个参数是一个闭包。</span> public <span class="token2">function</span> <span class="token6">handle</span><span class="token1">(</span>$request<span class="token1">,</span> \Closure $next<span class="token1">)</span> <span class="token1">{</span> <span class="token5">// 判断session里面有用户信息,没有说明没有登录,跳转到登录页</span> <span class="token2">if</span> <span class="token1">(</span><span class="token">!</span>$request<span class="token">-</span><span class="token">></span><span class="token6">session</span><span class="token1">(</span><span class="token4">'User'</span><span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token6">redirect</span><span class="token1">(</span><span class="token1">(</span>string<span class="token1">)</span><span class="token6">addons_url</span><span class="token1">(</span><span class="token4">'login/index'</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">return</span> $<span class="token6">next</span><span class="token1">(</span>$request<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` `前置中间件`:写在请求具体操作前为前置中间件,即:`$next($request);`前,如上面所示 `后置中间件`:执行完请求后为后置中间件,即:`$next($request);`后面 ``` <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\middleware<span class="token1">;</span> class <span class="token6">Login</span> <span class="token1">{</span> <span class="token5">// handle 为入口执行方法,必须。第一个参数是`Request`对象,第二个参数是一个闭包。</span> public <span class="token2">function</span> <span class="token6">handle</span><span class="token1">(</span>$request<span class="token1">,</span> \Closure $next<span class="token1">)</span> <span class="token1">{</span> $response <span class="token">=</span> $<span class="token6">next</span><span class="token1">(</span>$request<span class="token1">)</span><span class="token1">;</span> <span class="token5">// 需要的操作</span> <span class="token1">.</span><span class="token1">.</span><span class="token1">.</span> <span class="token5">// 返回响应</span> <span class="token2">return</span> $response<span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` 中间件传值到控制器方法 ``` <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\middleware<span class="token1">;</span> class <span class="token6">Login</span> <span class="token1">{</span> <span class="token5">// handle 为入口执行方法,必须。第一个参数是`Request`对象,第二个参数是一个闭包。</span> public <span class="token2">function</span> <span class="token6">handle</span><span class="token1">(</span>$request<span class="token1">,</span> \Closure $next<span class="token1">)</span> <span class="token1">{</span> $request<span class="token">-</span><span class="token">></span>Demo <span class="token">=</span> <span class="token3">1</span><span class="token1">;</span> <span class="token5">// 控制器里面 request()->Demo 即可获取值</span> <span class="token2">return</span> $<span class="token6">next</span><span class="token1">(</span>$request<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ## 使用中间件 执行顺序分别为: > 插件全局中间件->控制器中间件 ## 插件全局中间件 内置有插件全局中间件,可通过`addonMiddleware`事件来执行。 ## 控制器中间件 控制器中间件在插件控制器里面注册,如下,定义后访问Index控制器的任意方法都会经过中间件。 ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php namespace addons\demo\controller<span class="token1">;</span> use think\addons\Controller<span class="token1">;</span> class <span class="token6">Index</span> extends <span class="token6">Controller</span> <span class="token1">{</span> <span class="token5">// 控制器中间件</span> public $middleware <span class="token">=</span> <span class="token1">[</span> \addons\demo\middleware\Login<span class="token1">:</span><span class="token1">:</span>class <span class="token1">]</span><span class="token1">;</span> public <span class="token2">function</span> <span class="token6">index</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> public <span class="token2">function</span> <span class="token6">login</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> ``` ``` 使用except/only来设置生效的操作 only 示例 ``` <pre class="calibre16">``` <span class="token5">// 控制器中间件</span> public $middleware <span class="token">=</span> <span class="token1">[</span> \addons\demo\middleware\Login<span class="token1">:</span><span class="token1">:</span>class<span class="token">=</span><span class="token">></span><span class="token1">[</span><span class="token4">'only'</span><span class="token">=</span><span class="token">></span><span class="token1">[</span><span class="token4">'index'</span><span class="token1">,</span><span class="token4">'test'</span><span class="token1">]</span><span class="token1">]</span> <span class="token5">// Login中间仅对index、test方法拦截</span> <span class="token1">]</span><span class="token1">;</span> ``` ``` except 示例 ``` <pre class="calibre20">``` <span class="token5">// 控制器中间件</span> public $middleware <span class="token">=</span> <span class="token1">[</span> \addons\demo\middleware\Login<span class="token1">:</span><span class="token1">:</span>class<span class="token">=</span><span class="token">></span><span class="token1">[</span><span class="token4">'except'</span><span class="token">=</span><span class="token">></span><span class="token1">[</span><span class="token4">''</span>test'<span class="token1">]</span><span class="token1">]</span> <span class="token5">// 除了test方法,其他的所有操作方法都会经过Login中间件</span> <span class="token1">]</span><span class="token1">;</span> ``` ```