Skip to content

Commit ffcb916

Browse files
committed
minor #20003 [Messenger] Document #[AsMessage] attribute (pounard)
This PR was merged into the 7.2 branch. Discussion ---------- [Messenger] Document `#[AsMessage]` attribute Fixes #20002 Document the newly merged `#[AsMessage]` attribute symfony/symfony#57507 Commits ------- a381536 [Messenger] document the #[AsMessage] attribute
2 parents e5c526d + a381536 commit ffcb916

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

messenger.rst

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

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+
348397
Doctrine Entities in Messages
349398
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
350399

reference/attributes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ HttpKernel
7979
Messenger
8080
~~~~~~~~~
8181

82+
* :ref:`AsMessage <messenger-message-attribute>`
8283
* :ref:`AsMessageHandler <messenger-handler>`
8384

8485
RemoteEvent

0 commit comments

Comments
 (0)