thvvger/symfony-request-validator
Composer 安装命令:
composer require thvvger/symfony-request-validator
包简介
Validate symfony request and return JSON response
README 文档
README
Table of Contents
Symfony Request Validator
RequestValidator is a Symfony bundle that simplifies the process of validating request data and generating request classes with validation rules. It integrates with Symfony's Validator component and provides an easy way to generate request classes with built-in validation constraints.
Getting Started
This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.
Installation
- Install the package
composer require thvvger/symfony-request-validator
-
Configure the Services : After installing the bundle, you need to register the services for the
FileGeneratorandGenerateClassCommand.Add the following configuration to your
config/services.yaml:services: #.... # Register FileGenerator service Thvvger\RequestValidator\Services\FileGenerator: autowire: true autoconfigure: true arguments: $projectDir: '%kernel.project_dir%' # Inject the project directory path # Register GenerateClassCommand as a console command Thvvger\RequestValidator\Command\GenerateClassCommand: autowire: true autoconfigure: true arguments: $fileGenerator: '@Thvvger\RequestValidator\Services\FileGenerator' # Inject the FileGenerator service tags: - { name: 'console.command' } # Register the command for Symfony's console
- The
FileGeneratorservice is configured withautowireandautoconfigureto automatically inject dependencies. - The
GenerateClassCommandis registered as a console command, allowing you to execute it from the command line usingphp bin/console generate:class.
- The
-
Configuration
Ensure that the bundle is registered in `config/bundles.php:
return [ Thvvger\RequestValidator\RequestValidatorBundle::class => ['all' => true], ];
Usage
Generate a Request Class
To generate a request class, use the following command:
php bin/console make:request TestRequest
This will generate a PHP file in the src/Request/ directory with a base structure for your request class.
Example Generated Class
Here’s an example of a class generated after running the command:
namespace App\Request; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Type; use Thvvger\RequestValidator\BaseRequest; class TestRequest extends BaseRequest { #[NotBlank] #[Type('string')] public readonly string $name; // add other properties }
Example Usage in Your Controller
After generating the TestRequest class with the relevant validation logic, you can use it within a controller to handle incoming requests, perform validation, and execute any necessary logic (such as file generation).
#[Route('/test', methods: ['POST'])] public function test(TestRequest $request): JsonResponse { //... // Return a success message after the request is processed return new JsonResponse([ 'message' => 'Request exécuted succesfully', ]); }
If validation fails (e.g., missing or invalid file), a response like the following will be returned:
{
"message": "Validation error",
"errors": {
"fichier": "This value is invalid",
"someProperty": "This value cannot be blank."
}
}
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE.txt for more information.
Contact
Larry-Bill ADJE - @twitter_handle - thvger@gmail.com
Acknowledgments
thvvger/symfony-request-validator 适用场景与选型建议
thvvger/symfony-request-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 152 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 thvvger/symfony-request-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thvvger/symfony-request-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 152
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-08