定制 macpaw/request-dto-resolver 二次开发

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

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

macpaw/request-dto-resolver

Composer 安装命令:

composer require macpaw/request-dto-resolver

包简介

Request DTO resolver bundle

README 文档

README

Automatically resolves and validates Symfony HTTP request data (JSON, form-data, query parameters) into DTOs.

Features

  • Automatic resolution of request data into DTOs.
  • Seamless support for JSON, form-data, and query string parameters.
  • Built-in validation using the Symfony Form component.
  • Support for complex nested data structures.
  • Customizable parameter resolution order and field mapping.
  • Smart integration with other bundles that parse the request body.

Installation

composer require macpaw/request-dto-resolver

The bundle should be automatically registered in your config/bundles.php. If not, add it manually:

// config/bundles.php
return [
    RequestDtoResolver\RequestDtoResolverBundle::class => ['all' => true],
    // ...
];

Configuration

First, define an interface that your DTOs will implement. This allows the resolver to identify which arguments to process.

// src/DTO/RequestDtoInterface.php
namespace App\DTO;

interface RequestDtoInterface
{
}

Then, point the bundle to this interface in a configuration file:

# config/packages/request_dto_resolver.yaml
request_dto_resolver:
    target_dto_interface: App\DTO\RequestDtoInterface

How It Works

The resolver uses a combination of a DTO class and a Symfony Form to process and validate incoming request data.

  1. Controller Argument: You type-hint a controller argument with your DTO class (e.g., UserDto).
  2. FormType Attribute: You decorate the controller action with the #[FormType] attribute, specifying which Symfony Form to use for processing.
  3. Data Resolution: The resolver extracts data from the request based on the form's fields.
  4. Validation: The form validates the data against the constraints defined in your DTO.
  5. DTO Hydration: If validation passes, a new DTO instance is created and populated with the validated data.

Usage

1. Create a DTO

The DTO is a simple PHP class that implements your marker interface. Use Symfony's Validator components to define constraints.

// src/DTO/UserDto.php
namespace App\DTO;

use Symfony\Component\Validator\Constraints as Assert;

class UserDto implements RequestDtoInterface
{
    #[Assert\NotBlank]
    #[Assert\Length(min: 3)]
    public string $name;

    #[Assert\NotBlank]
    #[Assert\Email]
    public string $email;

    /** @var string[] */
    #[Assert\Count(min: 1)]
    #[Assert\All([
        new Assert\NotBlank,
        new Assert\Length(min: 2)
    ])]
    public array $tags = [];
}

2. Create a Form Type

The Form Type defines the structure of the expected request data and maps it to your DTO.

// src/Form/UserFormType.php
namespace App\Form;

use App\DTO\UserDto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UserFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class)
            ->add('email', EmailType::class)
            ->add('tags', CollectionType::class, [
                'entry_type' => TextType::class,
                'allow_add' => true,
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => UserDto::class,
        ]);
    }
}

3. Use in a Controller

In your controller, type-hint the action argument with your DTO class and add the #[FormType] attribute.

// src/Controller/UserController.php
namespace App\Controller;

use App\DTO\UserDto;
use App\Form\UserFormType;
use RequestDtoResolver\Attribute\FormType;
use Symfony\Component\HttpFoundation\JsonResponse;

class UserController
{
    #[FormType(UserFormType::class)]
    public function __invoke(UserDto $dto): JsonResponse
    {
        // $dto is now a validated and populated object
        return new JsonResponse([
            'name' => $dto->name,
            'email' => $dto->email,
            'tags' => $dto->tags,
        ]);
    }
}

Parameter Resolution

The resolver automatically extracts data from the request to populate the form. The source of the data depends on the request's Content-Type header and method.

Resolution Order

For each field defined in your Form Type, the resolver searches for a corresponding value in the following order:

  1. JSON Body: If the request has a Content-Type of application/json, the decoded JSON body is checked first.
  2. Query & Form Data: The resolver then checks request->query (for GET parameters) and request->request (for POST form data).
  3. Request Headers: Finally, it checks the request headers.

This order means that for a POST request with both a JSON body and query parameters, the values in the JSON body will take precedence.

Common Scenarios

  • POST with JSON Body: {"name": "John"} -> name is resolved from JSON.
  • POST with Form Data: name=John -> name is resolved from form data.
  • GET with Query Parameters: ?name=John -> name is resolved from query string.
  • GET with Content-Type: application/json: The resolver will correctly ignore the header and still pull data from the query string, preventing malformed body errors.
  • Request without Content-Type: The request is treated as a standard form/query request, and data is resolved from the query string.

Advanced Features

Custom Field Mapping

You can map request fields to different DTO properties using the lookupKey option in your Form Type. This is useful for handling request keys that don't match your DTO property names (e.g., user-id vs. userId).

Form Type Configuration:

// ...
$builder->add('userId', TextType::class, [
    'attr' => ['lookupKey' => 'user-id'],
]);
// ...

This configuration will map the user-id key from any source (JSON body, query, or header) to the userId form field.

Request Example:

POST /api/some-endpoint
Content-Type: application/json

{
    "user-id": 123
}

Integration with Other Bundles

This bundle is designed to work seamlessly with other bundles that parse the request body (e.g., FOSRestBundle). If the request body is already parsed and populated in $request->request, the resolver will automatically use this pre-parsed data instead of reading the raw body again.

This ensures:

  • No double-parsing of the request body.
  • Consistent validation and mapping rules.
  • Zero-configuration interoperability.

Error Handling

The bundle throws the following exceptions, which you can handle with a standard Symfony exception listener:

  • InvalidParamsDtoException: For validation errors (contains a ConstraintViolationList).
  • BadRequestHttpException: For a malformed JSON body.
  • UnsupportedMediaTypeHttpException: For an unsupported Content-Type.
  • MissingFormTypeAttributeException: When the #[FormType] attribute is missing on the controller action.

Contributing

Feel free to open issues and submit pull requests.

License

This bundle is released under the MIT license.

macpaw/request-dto-resolver 适用场景与选型建议

macpaw/request-dto-resolver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.85k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「resolver」 「request」 「dto」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 macpaw/request-dto-resolver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 macpaw/request-dto-resolver 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 30.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 15
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-05