thinkphp中间件是什么意思

互联网 19-6-29

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

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

定义中间件

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

php think make:middleware Check

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

<?php namespace app\http\middleware; 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 app\http\middleware; class Check {     public function handle($request, \Closure $next, $name)     {         if ($name == 'think') {             return redirect('index/think');         }         return $next($request);     } }

前置/后置中间件

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

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

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

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

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

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

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

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

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

return [     app\http\middleware\InAppCheck::class, ];

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

注册中间件

路由中间件

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

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

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

Route::rule('hello/:name','hello') ->middleware(app\http\middleware\Auth::class);

支持注册多个中间件

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

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

return [ 'auth'=>app\http\middleware\Auth::class,     'check'=>app\http\middleware\Check::class ];

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

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

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

return [ 'check'=>[     app\http\middleware\Auth::class,    app\http\middleware\Check::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); });

全局中间件

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

<?php return [ \app\http\middleware\Auth::class,     'Check',     'Hello', ];

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

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

<?php return [ [\app\http\middleware\Auth::class, 'admin'],     'Check',     'Hello:thinkphp', ];

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

模块中间件

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

控制器中间件

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

<?php namespace app\index\controller; use think\Controller; class Index extends Controller {     protected $middleware = ['Auth'];     public function index()     {         return 'index';     }     public function hello()     {         return 'hello';     } }

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

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

<?php namespace app\index\controller; use think\Controller; class Index extends Controller {     protected $middleware = [      'Auth' => ['except' => ['hello'] ],         'Hello' => ['only' => ['hello'] ],     ];     public function index()     {         return 'index';     }     public function hello()     {         return 'hello';     } }

中间件向控制器传参

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

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

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

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

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

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

以上就是thinkphp中间件是什么意思的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: ThinkPHP
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:怎么安装thinkphp

相关资讯