hyperf框架基础建设:中间件(统一接口参数校验)

1.引入组件包

composer require hyperf/validation

2.创建中间件:php ./bin/hyperf.php gen:middleware ValidateMiddleware

3.编写接口验证中间件(ValidateMiddleware.php):App\Middleware\ValidateMiddleware::class

<?php

declare(strict_types=1);

namespace App\Middleware;


use App\common\response\RespResult;
use App\common\response\SystemCode;
use App\Exception\ValidateException;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\HttpServer\Router\Handler;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;

class ValidateMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;


    /**
     * @var HttpResponse
     */
    protected $response;


    /**
     * @var RequestInterface
     */
    protected $request;



    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;


    public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request)
    {
        $this->container = $container;
        $this->response = $response;
        $this->request = $request;
    }


    /**
     * 业务逻辑处理
     * @param ServerRequestInterface $request
     * @param RequestHandlerInterface $handler
     * @return ResponseInterface
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        //获取控制器和方法
        list($LongController, $method) = $this->request->getAttribute(Dispatched::class)->handler->callback;
        $controller = substr(substr(strrchr($LongController,'\\'),1),0,-10);
        $res = $this->paramsValidate($controller,$method,$this->request);
        if($res["code"] === -1){
            //return RespResult::result(SystemCode::SYSTEM_ERROR_PARAM_NULL,$res["data"],[]);//方式1
            throw new ValidateException($res["data"],SystemCode::SYSTEM_ERROR_PARAM_NULL);//方式2
        }
        return $handler->handle($request);
    }


    /**
     * 参数校验函数
     * @param $controller
     * @param $method
     * @param $request
     * @return array
     */
    private function paramsValidate($controller,$method,$request){
        $nameSpace = '\App\Vilidate\\'.$controller;
        $ValidateObj = (new $nameSpace)->$method;
        $validator = $this->validationFactory->make(
            $request->all(),$ValidateObj["rule"],$ValidateObj["msg"]
        );
        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();
            return ["code"=>-1,"msg"=>"校验不通过!","data"=>$errorMessage];
        }
        //校验通过
        return ["code"=>0,"msg"=>"校验通过!","data"=>[]];
    }



}

4.定义全局中间件,所有接口都会执行此中间件(middlewares.php),如果是定义局部中间件这里不需要进行配置

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
 //全局中间件配置在该文件及对应的 Server Name 内,即该 Server 下的所有请求都会应用配置的全局中间件。
return [
    'http' => [
        \App\Middleware\ValidateMiddleware::class,
    ],
];

5.接口参数中的验证以及消息设置(验证组件:hyperf/validation是借鉴了laravel的illuminate/validation,所以两者使用方式基本一致),我们将这块逻辑放在App\Vilidate下,以test控制器为例,在Vilidate文件夹下创建test.php

<?php

namespace App\Vilidate;

/**
 * test控制器下的验证规则类
 */
class Test
{

    public $exceptionTest = [
        'rule'=>[
            'title' => 'required|max:5',
            'body' => 'required',
        ],
        'msg'=>[
            'title.required' => 'title参数不能为空!',
            'title.max' => 'title最大长度超限',
            'body.required' => 'body参数不能为空!',
        ]
    ];


}

6.使用方法(采用的注解方式访问和加载中间件)

/**
     * 异常处理测试
     * @RequestMapping(path="exceptionTest", methods="get,post")
     * @Middleware(ValidateMiddleware::class)
     */
    public function exceptionTest(){
        /*if(true){
            throw new ValidateException();
        }*/
        return RespResult::success();
    }

7.效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在.NET Core中,中间件负责处理HTTP请求和响应。中间件可以在请求到达控制器之前对请求进行操作,或在响应离开控制器之前对响应进行操作。 如果中间件无法正确读取请求参数,可能有几个原因导致这种情况。以下是一些可能的原因和解决方法: 1. 请求参数绑定问题:中间件可能无法正确绑定请求参数到控制器的操作方法参数上。可以检查控制器的操作方法参数的类型和名称是否与请求参数匹配,以确保正确绑定。 2. 请求体读取问题:如果请求中包含了请求体,中间件需要正确读取请求体才能获取请求参数。可以使用`Request.Body`对象来读取请求体,并使用适当的方法(如`StreamReader`)将其转换为字符串或JSON对象。 3. 请求头问题:某些请求参数可能包含在请求头中而不是请求体中。可以通过`Request.Headers`对象来读取请求头,并使用适当的方法(如`Get(key)`)获取特定请求头的值。 4. 中间件顺序问题:如果在中间件管道中使用了多个中间件,确保它们的顺序正确。有时,其他中间件可能已经处理了请求参数,导致当前中间件无法正确读取参数。可以尝试调整中间件的顺序,确保它在其他可能影响请求参数中间件之前执行。 5. 异常处理问题:如果中间件在读取请求参数时发生异常,可能会阻止其他中间件正确处理请求参数。可以通过在中间件代码中添加适当的错误处理逻辑来捕获并处理异常,以确保请求参数可以正确读取。 请根据具体情况检查以上可能的原因,并相应地调整中间件代码,以确保能正确读取请求参数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值