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/messenger.md
+77-1Lines changed: 77 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ Because the `messenger` attribute is `true`, when a `POST` is handled by API Pla
53
53
For this example, only the `POST` operation is enabled.
54
54
We use the `status` attribute to configure API Platform to return a [202 Accepted HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/202).
55
55
It indicates that the request has been received and will be treated later, without giving an immediate return to the client.
56
-
Finally, the `output` attribute is set to `false`, so the HTTP response that will be generated by API Platform will be empty, and the [serialization process](serialization.md) will be skipped.
56
+
Finally, the `output` attribute is set to `false`, so the HTTP response that will be generated by API Platform will be empty, and the [serialization process](serialization.md) will be skipped.
57
57
58
58
## Registering a Message Handler
59
59
@@ -92,3 +92,79 @@ It means that if you use a synchronous handler, the data returned by the `__invo
92
92
93
93
When a `DELETE` operation occurs, API Platform automatically adds a `ApiPlatform\Core\Bridge\Symfony\Messenger\RemoveStamp`["stamp"](https://symfony.com/doc/current/components/messenger.html#adding-metadata-to-messages-envelopes) instance to the "envelope".
94
94
To differentiate typical persists calls (create and update) and removal calls, check for the presence of this stamp using [a custom "middleware"](https://symfony.com/doc/current/components/messenger.html#adding-metadata-to-messages-envelopes).
95
+
96
+
## Using Messenger with an Input Object
97
+
98
+
Set the `messenger` attribute to `input`, and API Platform will automatically dispatch the given Input as a message instead of the Resource. Indeed, it'll add a default `DataTransformer` ([see input/output documentation](./dto.md)) that handles the given `input`.
99
+
100
+
```php
101
+
<?php
102
+
103
+
// api/src/Entity/User.php
104
+
105
+
namespace App\Entity;
106
+
107
+
use ApiPlatform\Core\Annotation\ApiResource;
108
+
use App\Dto\ResetPasswordRequest;
109
+
110
+
/**
111
+
* @ApiResource(
112
+
* collectionOperations={
113
+
* "post"={"status"=202}
114
+
* },
115
+
* itemOperations={},
116
+
* messenger=true,
117
+
* input=ResetPasswordRequest::class,
118
+
* output=false
119
+
* )
120
+
*/
121
+
final class User
122
+
{
123
+
}
124
+
```
125
+
126
+
Where `ResetPasswordRequest` would be:
127
+
128
+
```php
129
+
<?php
130
+
131
+
// api/src/Dto/ResetPasswordRequest.php
132
+
133
+
namespace App\Dto;
134
+
use Symfony\Component\Validator\Constraints as Assert;
135
+
136
+
final class ResetPasswordRequest
137
+
{
138
+
/**
139
+
* @var string
140
+
* @Assert\NotBlank
141
+
*/
142
+
public $var;
143
+
}
144
+
```
145
+
146
+
For this example, only the `POST` operation is enabled on `/users`.
147
+
As above, we use the `status` attribute to configure API Platform to return a [202 Accepted HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/202).
148
+
It indicates that the request has been received and will be treated later, without giving an immediate return to the client.
149
+
Finally, the `output` attribute is set to `false`, so the HTTP response that will be generated by API Platform will be empty, and the [serialization process](serialization.md) will be skipped.
150
+
151
+
In this case, when a `POST` request is issued on `/users` the message handler will receive an `App\Dto\ResetPasswordRequest` object instead a `User` because we specified it as `input` and set `messenger=input`:
0 commit comments