Skip to content

Commit b0dd4fa

Browse files
committed
[Messenger] document the #[AsMessage] attribute
1 parent 403f7d4 commit b0dd4fa

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

messenger.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,57 @@ to multiple transports:
345345
name as its only argument. For more information about stamps, see
346346
`Envelopes & Stamps`_.
347347

348+
Configuring Routing Using Attributes
349+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
350+
351+
You can optionally use the `#[AsMessage]` attribute to configure message transport:
352+
353+
.. code-block:: php
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+
.. code-block:: yaml
383+
384+
# config/packages/messenger.yaml
385+
when@dev:
386+
framework:
387+
messenger:
388+
routing:
389+
# override class attribute
390+
'App\Message\SmsNotification': sync
391+
392+
.. tip::
393+
394+
The `$transport` parameter can be either a `string` or an `array`
395+
or `string`: configuring multiple transports is possible.
396+
You may also repeat the attribute if you prefer instead of using
397+
an array.
398+
348399
Doctrine Entities in Messages
349400
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
350401

0 commit comments

Comments
 (0)