xu767142206/x-validated
最新稳定版本:v2.0.0-rc
Composer 安装命令:
composer require xu767142206/x-validated
包简介
Request Data Validated For Hyperf
关键字:
README 文档
README
简易版注解验证库 for Hyperf, 基于 hyperf/validation
起因:一直眼馋springboot的validation库,正好php8也有了原生的注解,就想到了套娃的方式简易实现 后面有闲情了再想办法自己实现吧
安装
composer require xu767142206/x-validated
配置
1.config/autoload/exceptions.php 也可以自己实现ValidationException的处理
return [ 'handler' => [ 'http' => [ //.... Hyperf\Validation\ValidationExceptionHandler::class, //.... ], ], ];
使用
dto模型的定义
dto模型必须继承 XValidated\ValidatedData
模型的定义
use XValidated\Annotation\Rule; use XValidated\ValidatedData; class LoginDto extends ValidatedData { #[Rule(values: ["required"])] public int $phone; #[Rule(values: ["required"], alias: "pass_word", messages: ['required' => "密码不能为空"])] public string $password; }
rule规则可以查看 hyperf/validation库的验证规则 验证规则
然后创建控制器 IndexController:
use XValidated\Annotation\Validated; use App\Model\LoginDto; use Hyperf\HttpServer\Annotation\AutoController; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Contract\RequestInterface; #[AutoController] class IndexController extends AbstractController { #[RequestMapping(path: "index", methods: "get,post")] #[Validated] public function index(RequestInterface $request, LoginDto $loginDto) { var_dump($loginDto->toArray()); return [ 'param' => $request->getServerParams(), ]; } }
接着可以快乐的写代码了
License
MIT
统计信息
- 总下载量: 395
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-19