ThinkPHP 中间件
从5.1.6+
版本开始 正式引入中间件的支持,中间件主要用于拦截或过滤应用的HTTP
请求 并进行必要的业务处理。
定义中间件
可以通过命令行指令快速生成中间件
php think make:middleware Check
这个指令会 application/http/middleware
目录下面生成个Check
中间件。
<?phpnamespace 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
。
在某些需求下 可以使用第三个参数传入额外的参数。
<?phpnamespace apphttpmiddleware;class Check{public function handle($request, Closure $next, $name){if ($name == 'think') {return redirect('index/think'); }return $next($request); } }
前置/后置中间件
中间件是在请求具体的操作之前还是之后执行 完全取决于中间件的定义本身。
下面是个前置行为的中间件
<?phpnamespace apphttpmiddleware;class Before{public function handle($request, Closure $next){// 添加中间件执行代码return $next($request); } }
下面是个后置行为的中间件
<?phpnamespace apphttpmiddleware;class After{public function handle($request, Closure $next){ $response = $next($request);// 添加中间件执行代码return $response; } }
注册中间件
全局中间件
你可以在应用目录下面定义middleware.php
文件 使用下面的方式:
<?phpreturn [ apphttpmiddlewareAuth::class,'Check','Hello',];
中间件的注册应该使用完整的类名 如果没有指定命名空间则使用apphttpmiddleware
作为命名空间。
全局中间件的执行顺序就是定义顺序。可以在定义全局中间件的时候传入中间件参数 支持两种方式传入。
<?phpreturn [ [apphttpmiddlewareAuth::class, 'admin'],'Check','Hello:thinkphp',];
上面的定义表示 给Auth
中间件传入admin
参数 给Hello
中间件传入thinkphp
参数。
V5.1.8+
版本开始 支持模块中间件定义 你可以直接在模块目录下面增加middleware.php
文件 定义方式和应用中间件定义一样 只是只会在该模块下面生效。
路由中间件
最常用的中间件注册方式是注册路由中间件
Route::rule('hello/:name','hello')->middleware('Auth');
或使用完整的中间件类名
Route::rule('hello/:name','hello') ->middleware(apphttpmiddlewareAuth::class);
支持注册多个中间件
Route::rule('hello/:name','hello')->middleware(['Auth', 'Check']);
V5.1.7+
版本 你可以直接在应用配置目录下的middleware.php
中先预定义中间件(其实就是增加别名标识) 例如:
return ['auth'=>apphttpmiddlewareAuth::class,'check'=>apphttpmiddlewareCheck::class];
然后直接在路由中使用中间件别名注册
Route::rule('hello/:name','hello')->middleware(['auth', 'check']);
V5.1.8+
版本开始 可以支持使用别名定义一组中间件 例如:
return ['check'=>[ apphttpmiddlewareAuth::class, apphttpmiddlewareCheck::class], ];
然后 直接使用下面的方式注册中间件
Route::rule('hello/:name','hello')->middleware('check');
支持对路由分组注册中间件
Route::group('hello', function(){ Route::rule('hello/:name','hello'); })->middleware('Auth');
V5.1.8+
版本开始支持对某个域名注册中间件
Route::domain('admin', function(){// 注册域名下的路由规则})->middleware('Auth');
如果需要传入额外参数给中间件 可以使用
Route::rule('hello/:name','hello')->middleware('Auth:admin');
如果使用的是常量方式定义 可以在第二个参数传入中间件参数。
Route::rule('hello/:name','hello') ->middleware(Auth::class, 'admin');
如果需要定义多个中间件 使用数组方式
Route::rule('hello/:name','hello') ->middleware([Auth::class, 'Check']);
可以统一传入同个额外参数
Route::rule('hello/:name','hello') ->middleware([Auth::class, 'Check'], 'admin');
或单独指定中间件参数。
Route::rule('hello/:name','hello') ->middleware(['Auth:admin', 'Check:editor']);
使用闭包定义中间件
你不一定要使用中间件类 在某些简单的场合你可以使用闭包定义中间件 但闭包函数必须返回Response
对象实例。
Route::group('hello', function(){ Route::rule('hello/:name','hello'); })->middleware(function($request,Closure $next){if ($request->param('name') == 'think') {return redirect('index/think'); } return $next($request); });
尊贵的董事大人
英文标题不为空时 视为本栏投稿
需要关键字 描述 英文标题