hyperf/validation 衍生于 illuminate/validation,我们对它进行了一些改造,但保持了验证规则的相同。在这里感谢一下 Laravel 开发组,实现了如此强大好用的验证器组件。
1. 引入组件包
composer require hyperf/validation
2. 添加中间件
您需要为使用到验证器组件的 Server 在 config/autoload/middlewares.php 配置文件加上一个全局中间件 Hyperf\Validation\Middleware\ValidationMiddleware 的配置,如下为 http Server 加上对应的全局中间件的示例:
<?php return [ // 下面的 http 字符串对应 config/autoload/server.php 内每个 server 的 name 属性对应的值,意味着对应的中间件配置仅应用在该 Server 中 'http' => [ // 数组内配置您的全局中间件,顺序根据该数组的顺序 \Hyperf\Validation\Middleware\ValidationMiddleware::class // 这里隐藏了其它中间件 ], ];
3. 添加异常处理器
异常处理器主要对 Hyperf\Validation\ValidationException 异常进行处理,我们提供了一个 Hyperf\Validation\ValidationExceptionHandler 来进行处理,您需要手动将这个异常处理器配置到您的项目的 config/autoload/exceptions.php 文件内,当然,您也可以自定义您的异常处理器。
return [ 'handler' => [ 'http' => [ \Hyperf\Validation\ValidationExceptionHandler::class, Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class, App\Exception\Handler\AppExceptionHandler::class, ], ], ];
4. 创建验证器语言文件
php bin/hyperf.php vendor:publish hyperf/translation php bin/hyperf.php vendor:publish hyperf/validation
5. 使用:表单验证类
可以通过执行下面的命令创建一个名为 FooRequest 的表单验证类:
php bin/hyperf.php gen:request FooRequest
表单验证类会生成于 app\Request 目录下,如果该目录不存在,运行命令时会自动创建目录。
定义规则
<?php
declare(strict_types=1);
namespace App\Request;
use Hyperf\Validation\Request\FormRequest;
class FooRequest 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', 'string'],
'password' => ['required', 'min:6']
];
}
/**
* 验证的各字段的含义
* @return array|string[]
*/
public function attributes(): array
{
return [
'name' => '用户名',
'password' => '密码'
];
}
}依赖注入的方式验证
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Request\FooRequest;
class IndexController extends AbstractController
{
public function test(FooRequest $request)
{
// 返回验证通过的字段
return $request->validated();
}
}使用:通过表单验证对象
<?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
*/
namespace App\Controller;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
class IndexController
{
#[Inject]
protected ValidatorFactoryInterface $validationFactory;
public function index(RequestInterface $request)
{
// 表演的验证规则
$validator = $this->validationFactory->make(
$request->all(),
[
'name' => ['required', 'string'],
'password' => ['required', 'min:6'],
]
);
// 判断是否有出错
if ($validator->fails()){
// 错误信息
return [
'error' => $validator->errors()->first()
];
}
// 返回验证通过的字段
return $validator->validated();
}
}
路由:
Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
访问:
127.0.0.1:9501
本文为崔凯原创文章,转载无需和我联系,但请注明来自冷暖自知一抹茶ckhttp://www.cksite.cn