Skip to content

Commit 3c9e4b3

Browse files
authored
Merge pull request #768 from soyuka/document-messenger-input
Document messenger input
2 parents 16e3e24 + dc130f3 commit 3c9e4b3

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

core/messenger.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Because the `messenger` attribute is `true`, when a `POST` is handled by API Pla
5353
For this example, only the `POST` operation is enabled.
5454
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).
5555
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.
5757

5858
## Registering a Message Handler
5959

@@ -92,3 +92,79 @@ It means that if you use a synchronous handler, the data returned by the `__invo
9292

9393
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".
9494
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`:
152+
153+
```php
154+
<?php
155+
156+
// api/src/Handler/ResetPasswordRequestHandler.php
157+
158+
namespace App\Handler;
159+
160+
use App\Dto\ResetPasswordRequest;
161+
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
162+
163+
final class ResetPasswordRequestHandler implements MessageHandlerInterface
164+
{
165+
public function __invoke(ResetPasswordRequest $forgotPassword)
166+
{
167+
// do something with the resource
168+
}
169+
}
170+
```

0 commit comments

Comments
 (0)