搜索
首页php框架ThinkPHPthinkphp中间件是什么意思

thinkphp中间件是什么意思

Jun 29, 2019 pm 01:31 PM
thinkphp

thinkphp中间件是什么意思

从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(&#39;name&#39;) == &#39;think&#39;) {
            return redirect(&#39;index/think&#39;);
        }
        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 == &#39;think&#39;) {
            return redirect(&#39;index/think&#39;);
        }
        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(&#39;~micromessenger~i&#39;, $request->header(&#39;user-agent&#39;))) {
            $request->InApp = &#39;WeChat&#39;;
        } else if (preg_match(&#39;~alipay~i&#39;, $request->header(&#39;user-agent&#39;))) {
            $request->InApp = &#39;Alipay&#39;;
        }
        return $next($request);
    }
}

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

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

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

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

注册中间件

路由中间件

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

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware(&#39;Auth&#39;);

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

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

支持注册多个中间件

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

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

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

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

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

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

return [
&#39;check&#39;=>[
    app\http\middleware\Auth::class,
   app\http\middleware\Check::class
    ],
];

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

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware(&#39;check&#39;);

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

Route::group(&#39;hello&#39;, function(){
Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;);
})->middleware(&#39;Auth&#39;);

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

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

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

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware(&#39;Auth:admin&#39;);

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

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware(Auth::class, &#39;admin&#39;);

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

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware([Auth::class, &#39;Check&#39;]);

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

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware([Auth::class, &#39;Check&#39;], &#39;admin&#39;);

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

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware([&#39;Auth:admin&#39;, &#39;Check:editor&#39;]);

使用闭包定义中间件

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

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

全局中间件

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

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

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

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

<?php
return [
[\app\http\middleware\Auth::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 app\index\controller;
use think\Controller;
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 app\index\controller;
use think\Controller;
class Index extends Controller
{
    protected $middleware = [ 
    &#39;Auth&#39; => [&#39;except&#39; => [&#39;hello&#39;] ],
        &#39;Hello&#39; => [&#39;only&#39; => [&#39;hello&#39;] ],
    ];
    public function index()
    {
        return &#39;index&#39;;
    }
    public function hello()
    {
        return &#39;hello&#39;;
    }
}

中间件向控制器传参

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

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

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

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

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

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

以上是thinkphp中间件是什么意思的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。