|
2 | 2 |
|
3 | 3 | ## How to Use a DTO for Writing
|
4 | 4 |
|
| 5 | +!> The following isn't recommended anymore, please use [Input/Output](specify-an-input-and-or-output-class) instead |
| 6 | + |
5 | 7 | Sometimes it's easier to use a DTO than an Entity when performing simple
|
6 | 8 | operation. For example, the application should be able to send an email when
|
7 | 9 | someone has lost its password.
|
@@ -126,6 +128,8 @@ services:
|
126 | 128 |
|
127 | 129 | ## How to Use a DTO for Reading
|
128 | 130 |
|
| 131 | +!> The following isn't recommended anymore, please use [Input/Output](specify-an-input-and-or-output-class) instead |
| 132 | + |
129 | 133 | Sometimes, you need to retrieve data not related to an entity.
|
130 | 134 | For example, the application can send the
|
131 | 135 | [list of supported locales](https://github.com/symfony/demo/blob/master/config/services.yaml#L6)
|
@@ -350,3 +354,68 @@ use Swagger\Annotations as SWG;
|
350 | 354 | */
|
351 | 355 | public function __invoke()
|
352 | 356 | ```
|
| 357 | + |
| 358 | +### Specify an Input and/or Output Class |
| 359 | + |
| 360 | +For a given resource class, you may want to have a different representation of this class as input (write) or output (read). |
| 361 | +To do so, a resource can take an input and/or an output class: |
| 362 | + |
| 363 | +```php |
| 364 | +<?php |
| 365 | +// api/src/Entity/Data.php |
| 366 | + |
| 367 | +namespace App\Entity; |
| 368 | + |
| 369 | +use ApiPlatform\Core\Annotation\ApiResource; |
| 370 | +use App\Dto\Input; |
| 371 | +use App\Dto\Output; |
| 372 | + |
| 373 | +/** |
| 374 | + * @ApiResource( |
| 375 | + * inputClass=Input::class, |
| 376 | + * outputClass=Output::class, |
| 377 | + * ) |
| 378 | + */ |
| 379 | +final class Data |
| 380 | +{ |
| 381 | +} |
| 382 | +``` |
| 383 | + |
| 384 | +The `input_class` attribute will be used in the denormalization phase, when transforming the user data to a resource instance. |
| 385 | +The `output_class` attribute will be used in the normalization phase, this class represents how the `Data` resource will be given in the `Response`. |
| 386 | + |
| 387 | +To create a `Data`, we `POST` an `Input` and get back an `Output` in the response. |
| 388 | +For this example to work, one could use the following `DataPersister`: |
| 389 | + |
| 390 | +```php |
| 391 | +<?php |
| 392 | + |
| 393 | +namespace App\DataPersister; |
| 394 | + |
| 395 | +use ApiPlatform\Core\DataPersister\DataPersisterInterface; |
| 396 | +use App\Dto\Input; |
| 397 | +use App\Dto\Output; |
| 398 | + |
| 399 | +final class InputDataPersister implements DataPersisterInterface |
| 400 | +{ |
| 401 | + public function supports($data): bool |
| 402 | + { |
| 403 | + return $data instanceof Input; |
| 404 | + } |
| 405 | + |
| 406 | + public function persist($data) |
| 407 | + { |
| 408 | + $output = new Output(); |
| 409 | + $output->name = $data->name; |
| 410 | + $output->id = 1; |
| 411 | + |
| 412 | + return $output; |
| 413 | + } |
| 414 | + |
| 415 | + public function remove($data) |
| 416 | + { |
| 417 | + // TODO: implement removal |
| 418 | + return null; |
| 419 | + } |
| 420 | +} |
| 421 | +``` |
0 commit comments