php-collective/symfony-dto
Composer 安装命令:
composer require php-collective/symfony-dto
包简介
Symfony integration for php-collective/dto
README 文档
README
Symfony bundle integration for php-collective/dto.
Installation
composer require php-collective/symfony-dto
The bundle will be auto-configured if you're using Symfony Flex.
Manual Registration
If not using Flex, add to config/bundles.php:
return [ // ... PhpCollective\SymfonyDto\PhpCollectiveDtoBundle::class => ['all' => true], ];
Configuration
Create config/packages/php_collective_dto.yaml:
php_collective_dto: config_path: config/ # Path to DTO config files (relative to project root) output_path: src/Dto/ # Path for generated DTOs namespace: App\Dto # Namespace for generated DTOs typescript_output_path: assets/types # TypeScript output jsonschema_output_path: config/schemas # JSON Schema output enable_value_resolver: true # Enable controller DTO auto-resolution enable_automapper: true # Enable AutoMapper bridge (requires jolicode/automapper)
Usage
1. Initialize DTO configuration
bin/console dto:init
This creates a config/dto.php file with a sample DTO definition (PHP format is the default).
You can also use --format=xml or --format=yaml.
The generated config looks like:
use PhpCollective\Dto\Builder\Dto; use PhpCollective\Dto\Builder\Field; use PhpCollective\Dto\Builder\Schema; return Schema::create() ->dto(Dto::create('User')->fields( Field::int('id'), Field::string('name'), Field::string('email')->nullable(), )) ->toArray();
2. Generate DTOs
bin/console dto:generate
Options:
--dry-run- Preview changes without writing files--config-path- Override config path--output-path- Override output path--namespace- Override namespace-v- Verbose output
3. Generate TypeScript interfaces
bin/console dto:typescript bin/console dto:typescript --multiple-files --readonly
4. Generate JSON Schema
bin/console dto:jsonschema bin/console dto:jsonschema --multiple-files
5. Use your DTOs
use App\Dto\UserDto; $user = new UserDto([ 'id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com', ]); return $this->json($user->toArray());
DTO Mapping Helpers
use App\Dto\UserDto; use PhpCollective\SymfonyDto\Mapper\DtoMapper; $dto = DtoMapper::fromArray(['name' => 'Mark'], UserDto::class); $dtos = DtoMapper::fromIterable($rows, UserDto::class); $collection = DtoMapper::fromCollection($doctrineCollection, UserDto::class); // Generic pagination wrapper $pagination = DtoMapper::fromPaginated( items: $pageItems, total: $total, perPage: $perPage, page: $page, dtoClass: UserDto::class, );
JSON Response Helper
use PhpCollective\SymfonyDto\Http\DtoJsonResponse; return DtoJsonResponse::fromDto($dto); // or return DtoJsonResponse::fromCollection($dtos);
Controller DTO Resolution
When enable_value_resolver is enabled, you can use #[MapRequestDto] to map request data to DTOs:
use PhpCollective\SymfonyDto\Attribute\MapRequestDto; #[Route('/users', methods: ['POST'])] public function create(#[MapRequestDto] UserDto $dto): Response { // $dto is built from request data }
The source option controls where data comes from: body, query, request, or auto.
Collections
The bundle automatically registers Doctrine's ArrayCollection for DTO collection fields. Define collection fields with the [] suffix:
Field::array('roles', 'Role'), // Role[] collection Field::array('tags', 'string'), // string[] collection
After generating, collection fields use Doctrine's ArrayCollection class with its methods (filter, map, first, etc.).
Validation Bridge
Automatically convert DTO validation rules to Symfony Validator constraints:
use PhpCollective\SymfonyDto\Validation\DtoConstraintBuilder; use Symfony\Component\Validator\Validation; $constraint = DtoConstraintBuilder::fromDto(new UserDto()); $violations = Validation::createValidator()->validate($data, $constraint);
See Usage docs for details.
AutoMapper Bridge
For automatic entity-to-DTO mapping without manual field assignment, install the optional AutoMapper integration:
composer require jolicode/automapper
Then use DtoAutoMapperInterface in your services:
use PhpCollective\SymfonyDto\AutoMapper\DtoAutoMapperInterface; class UserController extends AbstractController { public function __construct( private DtoAutoMapperInterface $dtoMapper, ) {} #[Route('/users/{id}')] public function show(User $user): JsonResponse { $dto = $this->dtoMapper->toDto($user, UserDto::class); return $this->json($dto->toArray()); } }
See Usage docs for full documentation including bidirectional mapping.
Supported Config Formats
The bundle supports multiple config file formats:
dto.php- PHP format (default)dto.xml- XML formatdto.yml/dto.yaml- YAML formatdto/subdirectory with multiple files
License
MIT
php-collective/symfony-dto 适用场景与选型建议
php-collective/symfony-dto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「bundle」 「dto」 「data-transfer-object」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-collective/symfony-dto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-collective/symfony-dto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-collective/symfony-dto 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
PHP library for the MUMSYS project
A simple library that allows transform any kind of data to native php data or whatever
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-16