Hyperf2.1框架中验证器的使用流程步骤
官方手册上写着hyperf/validation 衍生于illuminate/validation,熟悉laravel的就不过多的介绍,不熟悉的自行搜索了解一下,我这里直接开始体验。
手册上也介绍了两种:
一.是创建表单验证类
对于复杂的验证场景,您可以创建一个 表单请求(FormRequest),表单请求是包含验证逻辑的一个自定义请求类,表单验证类会生成于 app\Request 目录下
二.手动创建验证器
如果您不想使用 表单请求(FormRequest) 的自动验证功能,可以通过注入 ValidatorFactoryInterface 接口类来获得验证器工厂类,然后通过 make 方法手动创建一个验证器实例
1.开始安装组件包
[[email protected] 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 组件的文件
[[email protected] 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.发布验证器组件的文件
[[email protected] 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 (就用登录控制器测试)
创建表单验证类
[[email protected] 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框架
[[email protected] 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;
}
一般简单的验证就用这个,方便
看看效果
相关文章