V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
caola
V2EX  ›  PHP

hyperf 验证器问题

  •  
  •   caola · 2022-04-24 12:44:12 +08:00 · 1905 次点击
    这是一个创建于 704 天前的主题,其中的信息可能已经有所发展或是发生改变。

    hyperf 的验证器,如何在控制器中获取到验证失败的信息,默认只是获取成功后的验证。

    10 条回复    2022-04-25 09:25:21 +08:00
    codefever
        1
    codefever  
       2022-04-24 13:24:41 +08:00
    安装 validation

    composer require hyperf/validation
    安装 translation

    composer require hyperf/translation
    生成 translation 配置文件

    php bin/hyperf.php vendor:publish hyperf/translation
    生成 validation 配置文件

    php bin/hyperf.php vendor:publish hyperf/validation
    生成验证器 LoginRequest

    php bin/hyperf.php gen:request LoginRequest
    控制器 app/Controller/IndexController.php

    <?php
    namespace App\Controller;

    use Hyperf\HttpServer\Annotation\AutoController;
    use App\Request\LoginRequest;

    /**
    * @AutoController();
    */
    class IndexController
    {
    public function index(LoginRequest $request){
    $validateData = $request->validated();
    return $validateData;
    }
    }
    验证器添加规则 app/Request/LoginRequest.php

    <?php

    declare(strict_types=1);

    namespace App\Request;

    use Hyperf\Validation\Request\FormRequest;

    class LoginRequest extends FormRequest
    {
    /**
    * Determine if the user is authorized to make this request.
    */
    public function authorize(): bool
    {
    return true;
    }

    /**
    * Get the validation rules that apply to the request.
    */
    public function rules(): array
    {
    return [
    'name'=>'required',
    'password'=>'required'
    ];
    }
    }
    添加验证器中间件

    <?php

    declare(strict_types=1);

    return [
    'http' => [
    \Hyperf\Validation\Middleware\ValidationMiddleware::class
    ],
    ];
    添加异常处理器

    <?php

    declare(strict_types=1);

    return [
    'handler' => [
    'http' => [
    Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
    App\Exception\Handler\AppExceptionHandler::class,
    Hyperf\Validation\ValidationExceptionHandler::class
    ],
    ],
    ];
    测试 1

    curl 118.195.173.53:9501/index/index
    name 字段是必须的
    测试 2

    curl 118.195.173.53:9501/index/index?name=huyongjian
    password 字段是必须
    测试 3

    curl 118.195.173.53:9501/index/index?name=huyongjian\&password=123456
    {
    "name": "huyongjian",
    "password": "123456"
    }
    codefever
        2
    codefever  
       2022-04-24 13:25:22 +08:00
    @codefever 忘了说,教程来自老鄢博客
    caola
        3
    caola  
    OP
       2022-04-24 13:47:42 +08:00
    @codefever 你是没理解我的意思啊,我是要在控制器里,获取验证不通过时的提示信息;
    比如用户名是必填的,那么就得获取 ->errors()->first() 第一个必填的提示信息。但这个只能是自定义手动创建验证器才能实现。
    我是想在不改为原本的验证器的情况下,拿到这个验证失败的信息,之后再完成其他操作
    abigeater
        4
    abigeater  
       2022-04-24 13:54:37 +08:00
    通过捕捉异常可以获取到验证失败的信息
    abigeater
        5
    abigeater  
       2022-04-24 13:55:59 +08:00
    补 4 楼,敲了一下就回复了,还没来得及贴文档地址: https://hyperf.wiki/2.2/#/zh-cn/exception-handler
    caola
        6
    caola  
    OP
       2022-04-24 14:11:29 +08:00
    @abigeater 定义异常处理器,怎么把信息抛回到控制器里,我不需要打印输出。
    只要放在一个变量里或者方法里,在控制能获取到就行
    abigeater
        7
    abigeater  
       2022-04-24 14:33:41 +08:00
    @caola Hyperf 的验证器是通过中间件实现的,在中间件已经被拦截抛异常出去了,走不进控制器层。
    想要在控制器层只能是手动创建验证器,或者修改原有的中间件,出错后不要抛异常,将异常存在上下文,走进控制器后从上下文拿到,再自己控制是否抛异常。
    AngryPanda
        8
    AngryPanda  
       2022-04-24 16:13:52 +08:00
    @abigeater #7 为啥我感觉这样的设计不是很合理?很多验证规则可能是与业务绑定的。这样的设计如何解决此类问题?
    abigeater
        9
    abigeater  
       2022-04-24 16:58:43 +08:00
    @AngryPanda 我也觉得很多验证规则是和业务绑定的,所以我在这层验证器一般只做参数必填和数据结构的校验,并且比较复杂的结构下 Laravel 验证器的效率已被验证较低;
    limingxinleo
        10
    limingxinleo  
       2022-04-25 09:25:21 +08:00
    @abigeater 这位同学说的很对了,

    另外就是如果你不想让框架捕获异常,可以直接在 控制器里,手动 new 验证器,然后自己验证,相关代码可以去看看 ValidationMiddleware 里的实现
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   961 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:46 · PVG 05:46 · LAX 14:46 · JFK 17:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.