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

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

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

thvvger/symfony-request-validator

最新稳定版本:1.4.9

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)

统计信息

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

GitHub 信息

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

其他信息

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