thinkphp中间件是什么

thinkphp是为了简化企业级应用开发和敏捷web应用开发而诞生的。最早诞生于2006年初,2007年元旦正式更名为thinkphp,并且遵循apache2开源协议发布。thinkphp从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进。

thinkphp中间件是什么

thinkphp中间件是什么?

从5.1.6+版本开始,正式引入中间件的支持。

中间件主要用于拦截或过滤应用的http请求,并进行必要的业务处理。

定义中间件

立即学习PHP免费学习笔记(深入)”;

可以通过命令行指令快速生成中间件

php think make:middleware Check

这个指令会 application/http/middleware目录下面生成一个Check中间件。

<?php namespace apphttpmiddleware; class Check {     public function handle($request, Closure $next)     {         if ($request->param('name') == 'think') {             return redirect('index/think');         }         return $next($request);     } }

中间件的入口执行方法必须是handle方法,而且第一个参数是Request对象,第二个参数是一个闭包

中间件handle方法的返回值必须是一个Response对象。

在这个中间件中我们判断当前请求的name参数等于think的时候进行重定向处理。否则,请求将进一步传递到应用中。要让请求继续传递到应用程序中,只需使用 $request 作为参数去调用回调函数 $next 。

在某些需求下,可以使用第三个参数传入额外的参数。

<?php namespace apphttpmiddleware; class Check {     public function handle($request, Closure $next, $name)     {         if ($name == &#39;think&#39;) {             return redirect(&#39;index/think&#39;);         }         return $next($request);     } }

前置/后置中间件

中间件是在请求具体的操作之前还是之后执行,完全取决于中间件的定义本身。

下面是一个前置行为的中间件

<?php namespace apphttpmiddleware; class Before {     public function handle($request, Closure $next)     {         // 添加中间件执行代码         return $next($request);     } }

下面是一个后置行为的中间件

<?php namespace apphttpmiddleware; class After {     public function handle($request, Closure $next)     { $response = $next($request);         // 添加中间件执行代码         return $response;     } }

来个比较实际的例子,我们需要判断当前浏览器环境是在微信或支付宝

namespace apphttpmiddleware; /**  * 访问环境检查,是否是微信或支付宝等  */ class InAppCheck {     public function handle($request, Closure $next)     {         if (preg_match('~micromessenger~i', $request-&gt;header('user-agent'))) {             $request-&gt;InApp = 'WeChat';         } else if (preg_match('~alipay~i', $request-&gt;header('user-agent'))) {             $request-&gt;InApp = 'Alipay';         }         return $next($request);     } }

然后在你的移动版的module里添加一个middleware.php文件

例如:/path/application/mobile/middleware.php

return [     apphttpmiddlewareInAppCheck::class, ];

然后在你的controller中可以通过$this->request->InApp获取相关的值

注册中间件

路由中间件

最常用的中间件注册方式是注册路由中间件

Route::rule('hello/:name','hello') -&gt;middleware('Auth');

或者使用完整的中间件类名

Route::rule('hello/:name','hello') -&gt;middleware(apphttpmiddlewareAuth::class);

支持注册多个中间件

Route::rule('hello/:name','hello') -&gt;middleware(['Auth', 'Check']);

V5.1.7+版本,你可以直接在应用配置目录下的middleware.php中先预定义中间件(其实就是增加别名标识),例如:

return [ 'auth'=&gt;apphttpmiddlewareAuth::class,     'check'=&gt;apphttpmiddlewareCheck::class ];

然后直接在路由中使用中间件别名注册

Route::rule('hello/:name','hello') -&gt;middleware(['auth', 'check']);

V5.1.8+版本开始,可以支持使用别名定义一组中间件,例如:

return [ 'check'=&gt;[     apphttpmiddlewareAuth::class,    apphttpmiddlewareCheck::class     ], ];

然后,直接使用下面的方式注册中间件

Route::rule('hello/:name','hello') -&gt;middleware('check');

支持对路由分组注册中间件

Route::group('hello', function(){ Route::rule('hello/:name','hello'); })-&gt;middleware('Auth');

V5.1.8+版本开始支持对某个域名注册中间件

Route::domain('admin', function(){ // 注册域名下的路由规则 })-&gt;middleware('Auth');

如果需要传入额外参数给中间件,可以使用

Route::rule('hello/:name','hello') -&gt;middleware('Auth:admin');

如果使用的是常量方式定义,可以在第二个参数传入中间件参数。

Route::rule('hello/:name','hello') -&gt;middleware(Auth::class, 'admin');

如果需要定义多个中间件,使用数组方式

Route::rule('hello/:name','hello') -&gt;middleware([Auth::class, 'Check']);

可以统一传入同一个额外参数

Route::rule('hello/:name','hello') -&gt;middleware([Auth::class, 'Check'], 'admin');

或者单独指定中间件参数。

Route::rule('hello/:name','hello') -&gt;middleware(['Auth:admin', 'Check:editor']);

使用闭包定义中间件

你不一定要使用中间件类,在某些简单的场合你可以使用闭包定义中间件,但闭包函数必须返回Response对象实例。

Route::group('hello', function(){ Route::rule('hello/:name','hello'); })-&gt;middleware(function($request,Closure $next){     if ($request-&gt;param('name') == 'think') {         return redirect('index/think');     }      return $next($request); });

全局中间件

你可以在应用目录下面定义middleware.php文件,使用下面的方式:

<?php return [ apphttpmiddlewareAuth::class,     &#39;Check&#39;,     &#39;Hello&#39;, ];

中间件的注册应该使用完整的类名,如果没有指定命名空间则使用apphttpmiddleware作为命名空间。

全局中间件的执行顺序就是定义顺序。可以在定义全局中间件的时候传入中间件参数,支持两种方式传入。

<?php return [ [apphttpmiddlewareAuth::class, &#39;admin&#39;],     &#39;Check&#39;,     &#39;Hello:thinkphp&#39;, ];

上面的定义表示 给Auth中间件传入admin参数,给Hello中间件传入thinkphp参数。

模块中间件

V5.1.8+版本开始,支持模块中间件定义,你可以直接在模块目录下面增加middleware.php文件,定义方式和应用中间件定义一样,只是只会在该模块下面生效。

控制器中间件

V5.1.17+版本开始,支持为控制器定义中间件。首先你的控制器需要继承系统的thinkController类,然后在控制器中定义middleware属性,例如:

<?php namespace appindexcontroller; use thinkController; class Index extends Controller {     protected $middleware = [&#39;Auth&#39;];     public function index()     {         return &#39;index&#39;;     }     public function hello()     {         return &#39;hello&#39;;     } }

当执行index控制器的时候就会调用Auth中间件,一样支持使用完整的命名空间定义。

如果需要设置控制器中间的生效操作,可以如下定义:

<?php namespace appindexcontroller; use thinkController; class Index extends Controller {     protected $middleware = [      &#39;Auth&#39; => ['except' =&gt; ['hello'] ],         'Hello' =&gt; ['only' =&gt; ['hello'] ],     ];     public function index()     {         return 'index';     }     public function hello()     {         return 'hello';     } }

中间件向控制器传参

可以通过给请求对象赋值的方式传参给控制器(或者其它地方),例如

<?php namespace apphttpmiddleware; class Hello {     public function handle($request, Closure $next)     {         $request->hello = 'ThinkPHP';                  return $next($request);     } }

注意,传递的变量名称不要和param变量有冲突。

然后在控制器的方法里面可以直接使用

public function index(Request $request) { return $request-&gt;hello; // ThinkPHP }

本文来自ThinkPHP框架技术文章栏目:http://www.php.cn/phpkj/thinkphp/

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享