Skip to content

Commit

Permalink
[Messenger] document the #[AsMessage] attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Sep 30, 2024
1 parent d810f6c commit a381536
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,55 @@ to multiple transports:
name as its only argument. For more information about stamps, see
`Envelopes & Stamps`_.

.. _messenger-message-attribute:

Configuring Routing Using Attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can optionally use the `#[AsMessage]` attribute to configure message transport::

// src/Message/SmsNotification.php
namespace App\Message;

use Symfony\Component\Messenger\Attribute\AsMessage;

#[AsMessage(transport: 'async')]
class SmsNotification
{
public function __construct(
private string $content,
) {
}

public function getContent(): string
{
return $this->content;
}
}

.. note::

If you configure routing with both configuration and attributes, the
configuration will take precedence over the attributes and override
them. This allows to override routing on a per-environment basis
for example:

.. code-block:: yaml
# config/packages/messenger.yaml
when@dev:
framework:
messenger:
routing:
# override class attribute
'App\Message\SmsNotification': sync

Check failure on line 389 in messenger.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Cache Warmup] In FrameworkExtension.php line 2288: Invalid Messenger routing configuration: class or interface "App\Message\Sm sNotification" not found.
.. tip::

The `$transport` parameter can be either a `string` or an `array`: configuring multiple
transports is possible. You may also repeat the attribute if you prefer instead of using
an array.

Doctrine Entities in Messages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions reference/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ HttpKernel
Messenger
~~~~~~~~~

* :ref:`AsMessage <messenger-message-attribute>`
* :ref:`AsMessageHandler <messenger-handler>`

RemoteEvent
Expand Down

0 comments on commit a381536

Please sign in to comment.