Skip to content

Commit 41d48dd

Browse files
docs(dto): improve dto documentation and support laravel (#2076)
1 parent 3a6ffae commit 41d48dd

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

core/dto.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Using Data Transfer Objects (DTOs)
22

3+
<p class="symfonycasts" style="text-align: center;"><a href="https://symfonycasts.com/api-platform-extending?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="Custom Resources screencast"><br>Watch the Custom Resources screencast</a></p>
4+
35
As stated in [the general design considerations](design.md), in most cases [the DTO pattern](https://en.wikipedia.org/wiki/Data_transfer_object) should be implemented using an API Resource class representing the public data model exposed through the API and [a custom State Provider](state-providers.md). In such cases, the class marked with `#[ApiResource]` will act as a DTO.
46

57
However, it's sometimes useful to use a specific class to represent the input or output data structure related to an operation. These techniques are useful to document your API properly (using Hydra or OpenAPI) and will often be used on `POST` operations.
@@ -10,8 +12,7 @@ Using an input, the request body will be denormalized to the input instead of yo
1012

1113
```php
1214
<?php
13-
// api/src/Dto/UserResetPasswordDto.php
14-
15+
// api/src/Dto/UserResetPasswordDto.php with Symfony or app/Dto/UserResetPasswordDto.php with Laravel
1516
namespace App\Dto;
1617

1718
use Symfony\Component\Validator\Constraints as Assert;
@@ -25,8 +26,7 @@ final class UserResetPasswordDto
2526

2627
```php
2728
<?php
28-
// api/src/Model/User.php
29-
29+
// api/src/Model/User.php with Symfony or app/Model/User.php with Laravel
3030
namespace App\Model;
3131

3232
use ApiPlatform\Metadata\Post;
@@ -41,8 +41,7 @@ And the processor:
4141

4242
```php
4343
<?php
44-
// api/src/State/UserResetPasswordProcessor.php
45-
44+
// api/src/State/UserResetPasswordProcessor.php with Symfony or app/State/UserResetPasswordProcessor.php with Laravel
4645
namespace App\State;
4746

4847
use App\Dto\UserResetPasswordDto;
@@ -73,14 +72,13 @@ final class UserResetPasswordProcessor implements ProcessorInterface
7372

7473
In some cases, using an input DTO is a way to avoid serialization groups.
7574

76-
## Use Messenger With an Input DTO
75+
## Use Symfony Messenger With an Input DTO
7776

7877
Let's use a message that will be processed by [Symfony Messenger](https://symfony.com/components/Messenger). API Platform has an [integration with messenger](./messenger.md), to use a DTO as input you need to specify the `input` attribute:
7978

8079
```php
8180
<?php
82-
// api/src/Model/SendMessage.php
83-
81+
// api/src/Model/SendMessage.php with Symfony or app/Model/SendMessage.php with Laravel
8482
namespace App\Model;
8583

8684
use ApiPlatform\Metadata\Post;
@@ -99,9 +97,8 @@ To return another representation of your data in a [State Provider](./state-prov
9997

10098
```php
10199
<?php
102-
// api/src/Entity/Book.php
103-
104-
namespace App\Entity;
100+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
101+
namespace App\ApiResource;
105102

106103
use ApiPlatform\Metadata\Get;
107104
use App\Dto\AnotherRepresentation;
@@ -113,8 +110,7 @@ class Book {}
113110

114111
```php
115112
<?php
116-
// api/src/State/BookRepresentationProvider.php
117-
113+
// api/src/State/BookRepresentationProvider.php with Symfony or app/State/BookRepresentationProvider.php with Laravel
118114
namespace App\State;
119115

120116
use App\Dto\AnotherRepresentation;
@@ -136,15 +132,15 @@ final class BookRepresentationProvider implements ProviderInterface
136132

137133
## Implementing a Write Operation With an Output Different From the Resource
138134

139-
For returning another representation of your data in a [State Processor](./state-processors.md), you should specify your processor class in the `processor` attribute and same for your `output`.
135+
For returning another representation of your data in a [State Processor](./state-processors.md), you should specify your processor class in
136+
the `processor` attribute and same for your `output`.
140137

141138
<code-selector>
142139

143140
```php
144141
<?php
145-
// api/src/Entity/Book.php
146-
147-
namespace App\Entity;
142+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
143+
namespace App\ApiResource;
148144

149145
use ApiPlatform\Metadata\Post;
150146
use App\Dto\AnotherRepresentation;
@@ -156,8 +152,9 @@ class Book {}
156152

157153
```yaml
158154
# api/config/api_platform/resources.yaml
155+
# The YAML syntax is only supported for Symfony
159156
resources:
160-
App\Entity\Book:
157+
App\ApiResource\Book:
161158
operations:
162159
ApiPlatform\Metadata\Post:
163160
output: App\Dto\AnotherRepresentation
@@ -167,6 +164,7 @@ resources:
167164
```xml
168165
<?xml version="1.0" encoding="UTF-8" ?>
169166
<!-- api/config/api_platform/resources.xml -->
167+
<!-- The XML syntax is only supported for Symfony -->
170168

171169
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
172170
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -188,7 +186,7 @@ Here the `$data` attribute represents an instance of your resource.
188186

189187
```php
190188
<?php
191-
// api/src/State/BookRepresentationProcessor.php
189+
// api/src/State/BookRepresentationProcessor.php with Symfony or app/State/BookRepresentationProcessor.php with Laravel
192190

193191
namespace App\State;
194192

0 commit comments

Comments
 (0)