侯体宗的博客
  • 首页
  • Hyperf2.1版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Hyperf2.1框架中验证器的使用流程步骤

swoole  /  管理员 发布于 1年前   413

官方手册上写着hyperf/validation 衍生于illuminate/validation,熟悉laravel的就不过多的介绍,不熟悉的自行搜索了解一下,我这里直接开始体验。


手册上也介绍了两种:

一.是创建表单验证类

对于复杂的验证场景,您可以创建一个 表单请求(FormRequest),表单请求是包含验证逻辑的一个自定义请求类,表单验证类会生成于 app\Request 目录下


二.手动创建验证器

如果您不想使用 表单请求(FormRequest) 的自动验证功能,可以通过注入 ValidatorFactoryInterface 接口类来获得验证器工厂类,然后通过 make 方法手动创建一个验证器实例


1.开始安装组件包

[root@hyperf hyperf-skeleton]# composer require hyperf/validation
Using version ^2.1 for hyperf/validation
./composer.json has been updated
Running composer update hyperf/validation
Loading composer repositories with package information
...
Generating optimized autoload files
> rm -rf runtime/container
68 packages you are using are looking for funding.
Use the `composer fund` command to find out more!


2.发布 Translation 组件的文件

[root@hyperf hyperf-skeleton]# php bin/hyperf.php vendor:publish hyperf/translation
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[hyperf/translation] publishes [config] successfully.


3.发布验证器组件的文件

[root@hyperf hyperf-skeleton]# php bin/hyperf.php vendor:publish hyperf/validation
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[hyperf/validation] publishes [zh_CN] successfully.
[hyperf/validation] publishes [en] successfully.


4.添加中间件 (在config/autoload/middlewares.php文件中)

return [
    'http' => [
        // 数组内配置您的全局中间件,顺序根据该数组的顺序
        \Hyperf\Validation\Middleware\ValidationMiddleware::class
    ],
];


5.添加异常处理器 (在config/autoload/exceptions.php文件中)

return [
    'handler' => [
        'http' => [
            \Hyperf\Validation\ValidationExceptionHandler::class,
        ],
    ],
];

这里测试方式1 (就用登录控制器测试) 

创建表单验证类

[root@hyperf hyperf-skeleton]# php bin/hyperf.php gen:request FooRequest
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Paginator\Listener\PageResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
App\Request\FooRequest created successfully.

启动hyperf框架

[root@hyperf hyperf-skeleton]# php bin/hyperf.php start

在app\Request\FooRequest.php中配置规则

    public function rules(): array
    {
        return [
            'email' => 'required|email',
            'password' => 'required|min:6|max:12',
        ];
    }


控制器UserController.php中使用 (其他代码已删除)

<?php
declare(strict_types=1);
namespace App\Controller;

use App\Request\FooRequest;
class UserController extends AbstractController
{
    public function login(FooRequest $request)
    {
            // 获取通过验证的数据...
            $validated = $request->validated();
     }
}

这就完了 通过不了规则 直接提示规则信息


我们在测试方式2  

手动创建验证器

同样的控制器UserController.php中使用

<?php
declare(strict_types=1);
namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
class UserController extends AbstractController
{
    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;
    public function login(RequestInterface $request)
    {
            // 获取通过验证的数据...
            //$validated = $request->validated();
            $validator = $this->validationFactory->make(
                $request->all(),
                [
                    'email' => 'required|email',
                    'password' => 'required|min:6|max:12',
                ]
            );
            if ($validator->fails()){
                // Handle exception
                $data = array('error' => 0, 'info' => $validator->errors()->first());
                return $data;
            }


一般简单的验证就用这个,方便 

看看效果

1.png




  • 上一条:
    Hyperf2.1版本自定义函数的编写流程步骤及使用
    下一条:
    如何才能使一个良好的结构在Laravel框架项目中呢?介绍一下HMVC模式跟nwidart/laravel modules这个包
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • TP(3/5)
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • hyperf2.1框架使用Dockerfile部署流程步骤(0个评论)
    • hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令(2个评论)
    • hyperf2.1+ElementUi实现后台登录单页demo(0个评论)
    • hyperf2.1中使用中间件方式实现跨域功能流程步骤(0个评论)
    • hyperf2.1+96qbhy/hyperf-auth组件安装使用(0个评论)
    • 近期文章
    • Laravel 9.13版本发布(0个评论)
    • beego+GeoLite2免费数据库获取ip地址经纬度等定位归属信息(0个评论)
    • redis安全配置之修改端口、添加密码流程步骤及启动使用(0个评论)
    • PHP + Memcache实现简单的统计当前在线人数功能(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • go语言中使用Signbit()函数判断一个整数是正数或负数(0个评论)
    • 删库跑路之一链家程序员删除公司9TB数据被判7年,望各大码农警之!(0个评论)
    • Laravel角色和权限:拦截器Gates和策略Policies的解释(0个评论)
    • Laravel 9.12版本发布(0个评论)
    • go语言中实现把数据写入文件函数WriteFile()编写(0个评论)
    • 近期评论
    • 博主 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 @路过的靓仔:cdn静态资源被墙,已修复..
    • GGGGGGGGG 在

      layui框架常用输入框介绍中评论 写的很好解决问题..
    • 路过的靓仔 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 剩下好多 wait 状态的..
    • 激光豆芽 在

      为什么你不能安逸?国内996为什么没有国外955香?中评论 国内现在无意义的内卷太多了..
    • 激光豆芽 在

      阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 厉害了..
    • 2017-09
    • 2020-03
    • 2020-06
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-11
    • 2022-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客