@@ -203,8 +203,23 @@ Routing Messages to a Transport
203203Now that you have a transport configured, instead of handling a message immediately,
204204you can configure them to be sent to a transport:
205205
206+ .. _messenger-message-attribute :
207+
206208.. configuration-block ::
207209
210+ .. code-block :: php-attributes
211+
212+ // src/Message/SmsNotification.php
213+ namespace App\Message;
214+
215+ use Symfony\Component\Messenger\Attribute\AsMessage;
216+
217+ #[AsMessage('async')]
218+ class SmsNotification
219+ {
220+ // ...
221+ }
222+
208223 .. code-block :: yaml
209224
210225 # config/packages/messenger.yaml
@@ -251,15 +266,26 @@ you can configure them to be sent to a transport:
251266 ;
252267 };
253268
269+ .. versionadded :: 7.2
270+
271+ The ``#[AsMessage] `` attribute was introduced in Symfony 7.2.
272+
254273Thanks to this, the ``App\Message\SmsNotification `` will be sent to the ``async ``
255274transport and its handler(s) will *not * be called immediately. Any messages not
256275matched under ``routing `` will still be handled immediately, i.e. synchronously.
257276
258277.. note ::
259278
260- You may use a partial PHP namespace like ``'App\Message\*' `` to match all
261- the messages within the matching namespace. The only requirement is that the
262- ``'*' `` wildcard has to be placed at the end of the namespace.
279+ If you configure routing with both YAML/XML/PHP configuration files and
280+ PHP attributes, the configuration always takes precedence over the class
281+ attribute. This behavior allows you to override routing on a per-environment basis.
282+
283+ .. note ::
284+
285+ When configuring the routing in separate YAML/XML/PHP files, you can use a partial
286+ PHP namespace like ``'App\Message\*' `` to match all the messages within the
287+ matching namespace. The only requirement is that the ``'*' `` wildcard has to
288+ be placed at the end of the namespace.
263289
264290 You may use ``'*' `` as the message class. This will act as a default routing
265291 rule for any message not matched under ``routing ``. This is useful to ensure
@@ -275,6 +301,27 @@ to multiple transports:
275301
276302.. configuration-block ::
277303
304+ .. code-block :: php-attributes
305+
306+ // src/Message/SmsNotification.php
307+ namespace App\Message;
308+
309+ use Symfony\Component\Messenger\Attribute\AsMessage;
310+
311+ #[AsMessage(['async', 'audit'])]
312+ class SmsNotification
313+ {
314+ // ...
315+ }
316+
317+ // if you prefer, you can also apply multiple attributes to the message class
318+ #[AsMessage('async')]
319+ #[AsMessage('audit')]
320+ class SmsNotification
321+ {
322+ // ...
323+ }
324+
278325 .. code-block :: yaml
279326
280327 # config/packages/messenger.yaml
@@ -345,55 +392,6 @@ to multiple transports:
345392 name as its only argument. For more information about stamps, see
346393 `Envelopes & Stamps `_.
347394
348- .. _messenger-message-attribute :
349-
350- Configuring Routing Using Attributes
351- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
352-
353- You can optionally use the `#[AsMessage] ` attribute to configure message transport::
354-
355- // src/Message/SmsNotification.php
356- namespace App\Message;
357-
358- use Symfony\Component\Messenger\Attribute\AsMessage;
359-
360- #[AsMessage(transport: 'async')]
361- class SmsNotification
362- {
363- public function __construct(
364- private string $content,
365- ) {
366- }
367-
368- public function getContent(): string
369- {
370- return $this->content;
371- }
372- }
373-
374- .. note ::
375-
376- If you configure routing with both configuration and attributes, the
377- configuration will take precedence over the attributes and override
378- them. This allows to override routing on a per-environment basis
379- for example:
380-
381- .. code-block :: yaml
382-
383- # config/packages/messenger.yaml
384- when@dev :
385- framework :
386- messenger :
387- routing :
388- # override class attribute
389- ' App\Message\SmsNotification ' : sync
390-
391- .. tip ::
392-
393- The `$transport ` parameter can be either a `string ` or an `array `: configuring multiple
394- transports is possible. You may also repeat the attribute if you prefer instead of using
395- an array.
396-
397395Doctrine Entities in Messages
398396~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
399397
0 commit comments