定制 thvvger/symfony-request-validator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thvvger/symfony-request-validator

Composer 安装命令:

composer require thvvger/symfony-request-validator

包简介

Validate symfony request and return JSON response

README 文档

README


Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

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.

(back to top)

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

  1. Install the package
    composer require thvvger/symfony-request-validator
    1. Configure the Services : After installing the bundle, you need to register the services for the FileGenerator and GenerateClassCommand.

      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 FileGenerator service is configured with autowire and autoconfigure to automatically inject dependencies.
      • The GenerateClassCommand is registered as a console command, allowing you to execute it from the command line using php bin/console generate:class.

(back to top)

Configuration

Ensure that the bundle is registered in `config/bundles.php:

    return [
        Thvvger\RequestValidator\RequestValidatorBundle::class => ['all' => true],
    ];

(back to top)

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."
   }
}

(back to top)

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Larry-Bill ADJE - @twitter_handle - thvger@gmail.com

(back to top)

Acknowledgments

(back to top)

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 thvvger/symfony-request-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 152
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-08