PHPackages                             php-collective/symfony-dto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. php-collective/symfony-dto

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

php-collective/symfony-dto
==========================

Symfony integration for php-collective/dto

0.1.4(2mo ago)01MITPHPPHP &gt;=8.2CI passing

Since Dec 16Pushed 2mo agoCompare

[ Source](https://github.com/php-collective/symfony-dto)[ Packagist](https://packagist.org/packages/php-collective/symfony-dto)[ RSS](/packages/php-collective-symfony-dto/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (18)Versions (12)Used By (0)

Symfony DTO Bundle
==================

[](#symfony-dto-bundle)

Symfony bundle integration for [php-collective/dto](https://github.com/php-collective/dto).

Installation
------------

[](#installation)

```
composer require php-collective/symfony-dto
```

The bundle will be auto-configured if you're using Symfony Flex.

### Manual Registration

[](#manual-registration)

If not using Flex, add to `config/bundles.php`:

```
return [
    // ...
    PhpCollective\SymfonyDto\PhpCollectiveDtoBundle::class => ['all' => true],
];
```

Configuration
-------------

[](#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
```

Usage
-----

[](#usage)

### 1. Initialize DTO configuration

[](#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

[](#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

[](#3-generate-typescript-interfaces)

```
bin/console dto:typescript
bin/console dto:typescript --multiple-files --readonly
```

### 4. Generate JSON Schema

[](#4-generate-json-schema)

```
bin/console dto:jsonschema
bin/console dto:jsonschema --multiple-files
```

### 5. Use your DTOs

[](#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
-------------------

[](#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
--------------------

[](#json-response-helper)

```
use PhpCollective\SymfonyDto\Http\DtoJsonResponse;

return DtoJsonResponse::fromDto($dto);
// or
return DtoJsonResponse::fromCollection($dtos);
```

Controller DTO Resolution
-------------------------

[](#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
-----------

[](#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
-----------------

[](#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](docs/README.md#validation-bridge) for details.

Supported Config Formats
------------------------

[](#supported-config-formats)

The bundle supports multiple config file formats:

- `dto.php` - PHP format (default)
- `dto.xml` - XML format
- `dto.yml` / `dto.yaml` - YAML format
- `dto/` subdirectory with multiple files

License
-------

[](#license)

MIT

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance86

Actively maintained with recent releases

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~20 days

Total

5

Last Release

71d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/39854?v=4)[Mark Scherer](/maintainers/dereuromark)[@dereuromark](https://github.com/dereuromark)

---

Top Contributors

[![dereuromark](https://avatars.githubusercontent.com/u/39854?v=4)](https://github.com/dereuromark "dereuromark (34 commits)")

---

Tags

symfonybundledata-transfer-objectdto

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/php-collective-symfony-dto/health.svg)

```
[![Health](https://phpackages.com/badges/php-collective-symfony-dto/health.svg)](https://phpackages.com/packages/php-collective-symfony-dto)
```

###  Alternatives

[winzou/state-machine-bundle

Bundle for the very lightweight yet powerful PHP state machine

34010.4M15](/packages/winzou-state-machine-bundle)[pentatrion/vite-bundle

Vite integration for your Symfony app

2725.3M13](/packages/pentatrion-vite-bundle)[rewieer/taskschedulerbundle

Task Scheduler with CRON for Symfony

63242.1k](/packages/rewieer-taskschedulerbundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
