Skip to content

[Messenger] Document #[AsMessage] attribute #20003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,55 @@
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
Loading