You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/dto.md
+18-20Lines changed: 18 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# Using Data Transfer Objects (DTOs)
2
2
3
+
<pclass="symfonycasts"style="text-align: center;"><ahref="https://symfonycasts.com/api-platform-extending?cid=apip"><imgsrc="../symfony/images/symfonycasts-player.png"alt="Custom Resources screencast"><br>Watch the Custom Resources screencast</a></p>
4
+
3
5
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.
4
6
5
7
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
10
12
11
13
```php
12
14
<?php
13
-
// api/src/Dto/UserResetPasswordDto.php
14
-
15
+
// api/src/Dto/UserResetPasswordDto.php with Symfony or app/Dto/UserResetPasswordDto.php with Laravel
15
16
namespace App\Dto;
16
17
17
18
use Symfony\Component\Validator\Constraints as Assert;
@@ -25,8 +26,7 @@ final class UserResetPasswordDto
25
26
26
27
```php
27
28
<?php
28
-
// api/src/Model/User.php
29
-
29
+
// api/src/Model/User.php with Symfony or app/Model/User.php with Laravel
30
30
namespace App\Model;
31
31
32
32
use ApiPlatform\Metadata\Post;
@@ -41,8 +41,7 @@ And the processor:
41
41
42
42
```php
43
43
<?php
44
-
// api/src/State/UserResetPasswordProcessor.php
45
-
44
+
// api/src/State/UserResetPasswordProcessor.php with Symfony or app/State/UserResetPasswordProcessor.php with Laravel
46
45
namespace App\State;
47
46
48
47
use App\Dto\UserResetPasswordDto;
@@ -73,14 +72,13 @@ final class UserResetPasswordProcessor implements ProcessorInterface
73
72
74
73
In some cases, using an input DTO is a way to avoid serialization groups.
75
74
76
-
## Use Messenger With an Input DTO
75
+
## Use Symfony Messenger With an Input DTO
77
76
78
77
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:
79
78
80
79
```php
81
80
<?php
82
-
// api/src/Model/SendMessage.php
83
-
81
+
// api/src/Model/SendMessage.php with Symfony or app/Model/SendMessage.php with Laravel
84
82
namespace App\Model;
85
83
86
84
use ApiPlatform\Metadata\Post;
@@ -99,9 +97,8 @@ To return another representation of your data in a [State Provider](./state-prov
99
97
100
98
```php
101
99
<?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;
105
102
106
103
use ApiPlatform\Metadata\Get;
107
104
use App\Dto\AnotherRepresentation;
@@ -113,8 +110,7 @@ class Book {}
113
110
114
111
```php
115
112
<?php
116
-
// api/src/State/BookRepresentationProvider.php
117
-
113
+
// api/src/State/BookRepresentationProvider.php with Symfony or app/State/BookRepresentationProvider.php with Laravel
118
114
namespace App\State;
119
115
120
116
use App\Dto\AnotherRepresentation;
@@ -136,15 +132,15 @@ final class BookRepresentationProvider implements ProviderInterface
136
132
137
133
## Implementing a Write Operation With an Output Different From the Resource
138
134
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`.
140
137
141
138
<code-selector>
142
139
143
140
```php
144
141
<?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;
148
144
149
145
use ApiPlatform\Metadata\Post;
150
146
use App\Dto\AnotherRepresentation;
@@ -156,8 +152,9 @@ class Book {}
156
152
157
153
```yaml
158
154
# api/config/api_platform/resources.yaml
155
+
# The YAML syntax is only supported for Symfony
159
156
resources:
160
-
App\Entity\Book:
157
+
App\ApiResource\Book:
161
158
operations:
162
159
ApiPlatform\Metadata\Post:
163
160
output: App\Dto\AnotherRepresentation
@@ -167,6 +164,7 @@ resources:
167
164
```xml
168
165
<?xml version="1.0" encoding="UTF-8" ?>
169
166
<!-- api/config/api_platform/resources.xml -->
167
+
<!-- The XML syntax is only supported for Symfony -->
0 commit comments