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
Our DataPersisters are called in chain, once a data persister is supported the chain breaks and API Platform assumes your data is persisted. You can call mutliple data persisters by implementing the `ResumableDataPersisterInterface`:
154
+
155
+
```php
156
+
namespace App\DataPersister;
157
+
158
+
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
159
+
use App\Entity\BlogPost;
160
+
161
+
final class BlogPostDataPersister implements ContextAwareDataPersisterInterface, ResumableDataPersisterInterface
162
+
{
163
+
public function supports($data, array $context = []): bool
164
+
{
165
+
return $data instanceof BlogPost;
166
+
}
167
+
168
+
public function persist($data, array $context = [])
169
+
{
170
+
// call your persistence layer to save $data
171
+
return $data;
172
+
}
173
+
174
+
public function remove($data, array $context = [])
175
+
{
176
+
// call your persistence layer to delete $data
177
+
}
178
+
179
+
// Once called this data persister will resume to the next one
180
+
public function resumable(array $context = []): bool
181
+
{
182
+
return true;
183
+
}
184
+
}
185
+
```
186
+
187
+
This is very useful when using [`Messenger` with API Platform](messenger.md) as you may want to do something asynchronously with the data but still call the default Doctrine data persister.
Copy file name to clipboardExpand all lines: core/messenger.md
+37-66Lines changed: 37 additions & 66 deletions
Original file line number
Diff line number
Diff line change
@@ -18,99 +18,81 @@ docker-compose exec php \
18
18
19
19
## Dispatching a Resource through the Message Bus
20
20
21
-
Set the `messenger` attribute to `true`, and API Platform will automatically dispatch the API Resource instance as a message using the message bus provided by the Messenger Component:
21
+
Set the `messenger` attribute to `true`, and API Platform will automatically dispatch the API Resource instance as a message using the message bus provided by the Messenger Component. The following example allows you to create a new `Person` in an asynchronous manner:
22
22
23
23
[codeSelector]
24
24
25
25
```php
26
26
<?php
27
-
// api/src/Entity/ResetPasswordRequest.php
27
+
// api/src/Entity/Person.php
28
28
29
29
namespace App\Entity;
30
30
31
31
use ApiPlatform\Core\Annotation\ApiResource;
32
32
use Symfony\Component\Validator\Constraints as Assert;
Because the `messenger` attribute is `true`, when a `POST` is handled by API Platform, the corresponding instance of the `ResetPasswordRequest` will be dispatched.
71
+
Because the `messenger` attribute is `true`, when a `POST` is handled by API Platform, the corresponding instance of the `Person` will be dispatched.
71
72
72
-
For this example, only the `POST` operation is enabled.
73
+
For this example, only the `POST` operation is enabled. We disabled the item operation using the `NotFoundAction`. A resource must have at least one item operation as it must be identified by an IRI, here the route `/people/1` exists, eventhough it returns a 404 status code.
73
74
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).
74
75
It indicates that the request has been received and will be treated later, without giving an immediate return to the client.
75
-
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.
76
-
77
-
**Note:** when using `messenger=true` ApiResource attribute in a Doctrine entity, the Doctrine DataPersister is not called. You must use the `messenger="persist"` ApiResource attribute.
78
-
79
-
**Note:** when using `messenger="input"` ApiResource attribute in a Doctrine entity, the Doctrine DataPersister is not called. You must use an array containing `persist` and `input` if you want it to be called, for example:
80
-
81
-
[codeSelector]
82
-
83
-
```php
84
-
/**
85
-
* @ApiResource(messenger={"persist", "input"})
86
-
*/
87
-
```
76
+
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.
88
77
89
-
```yaml
90
-
resources:
91
-
# ...
92
-
attributes:
93
-
messenger: ['persist', 'input']
94
-
```
95
-
96
-
[/codeSelector]
78
+
**Note:** when using `messenger=true` ApiResource attribute in a Doctrine entity, the Doctrine DataPersister is not called. If you want the Doctrine DataPersister to be called, you should implement a `ResumableDataPersisterInterface` [documented here](data-persisters.md).
97
79
98
80
## Registering a Message Handler
99
81
100
82
To process the message that will be dispatched, [a handler](https://symfony.com/doc/current/messenger.html#registering-handlers) must be created:
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
110
92
111
-
final class ResetPasswordRequestHandler implements MessageHandlerInterface
93
+
final class PersonHandler implements MessageHandlerInterface
112
94
{
113
-
public function __invoke(ResetPasswordRequest $forgotPassword)
95
+
public function __invoke(PersonHandler $person)
114
96
{
115
97
// do something with the resource
116
98
}
@@ -137,7 +119,7 @@ To differentiate typical persists calls (create and update) and removal calls, c
137
119
138
120
## Using Messenger with an Input Object
139
121
140
-
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`.
122
+
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`. In this example, we'll handle a `ResetPasswordRequest` on a custom operation on our `User` resource:
@@ -175,20 +151,15 @@ use Symfony\Component\Validator\Constraints as Assert;
175
151
176
152
final class ResetPasswordRequest
177
153
{
178
-
/**
179
-
* @var string
180
-
* @Assert\NotBlank
181
-
*/
182
-
public $var;
154
+
public string $username;
183
155
}
184
156
```
185
157
186
-
For this example, only the `POST` operation is enabled on `/users`.
187
158
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).
188
159
It indicates that the request has been received and will be treated later, without giving an immediate return to the client.
189
160
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.
190
161
191
-
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`:
162
+
In this case, when a `POST` request is issued on `/users/reset_password` 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