Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fdd8b86

Browse filesBrowse files
[Messenger] Autoconfigurable attributes
1 parent 6e83621 commit fdd8b86
Copy full SHA for fdd8b86

File tree

1 file changed

+40
-7
lines changed
Filter options

1 file changed

+40
-7
lines changed

‎messenger.rst

Copy file name to clipboardExpand all lines: messenger.rst
+40-7Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,19 @@ serialized::
5151
.. _messenger-handler:
5252

5353
A message handler is a PHP callable, the recommended way to create it is to
54-
create a class that implements :class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface`
55-
and has an ``__invoke()`` method that's type-hinted with the message class (or a
56-
message interface)::
54+
create a class using :class:`Symfony\\Component\\Messenger\\Attribute\\AsMessageHandler`
55+
attribute which has an ``__invoke()`` method that's type-hinted with the
56+
message class (or a message interface) or you can create a class without the attribute, by implementing
57+
:class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface`::
5758

5859
// src/MessageHandler/SmsNotificationHandler.php
5960
namespace App\MessageHandler;
6061

6162
use App\Message\SmsNotification;
62-
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
63+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
6364

64-
class SmsNotificationHandler implements MessageHandlerInterface
65+
#[AsMessageHandler]
66+
class SmsNotificationHandler
6567
{
6668
public function __invoke(SmsNotification $message)
6769
{
@@ -349,9 +351,10 @@ Then, in your handler, you can query for a fresh object::
349351

350352
use App\Message\NewUserWelcomeEmail;
351353
use App\Repository\UserRepository;
352-
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
354+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
353355

354-
class NewUserWelcomeEmailHandler implements MessageHandlerInterface
356+
#[AsMessageHandler]
357+
class NewUserWelcomeEmailHandler
355358
{
356359
private $userRepository;
357360

@@ -1769,6 +1772,36 @@ Customizing Handlers
17691772

17701773
.. _messenger-handler-config:
17711774

1775+
Configuring Handlers Using Attribute
1776+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1777+
1778+
You can configure your handler easily by passing options to the attribute::
1779+
1780+
// src/MessageHandler/SmsNotificationHandler.php
1781+
namespace App\MessageHandler;
1782+
1783+
use App\Message\OtherSmsNotification;
1784+
use App\Message\SmsNotification;
1785+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1786+
1787+
#[AsMessageHandler(fromTransport: 'async', priority: 10)]
1788+
class SmsNotificationHandler
1789+
{
1790+
public function __invoke(SmsNotification $message)
1791+
{
1792+
// ...
1793+
}
1794+
}
1795+
1796+
1797+
Possible options to configure with the attribute are:
1798+
1799+
* ``bus``
1800+
* ``fromTransport``
1801+
* ``handles``
1802+
* ``method``
1803+
* ``priority``
1804+
17721805
Manually Configuring Handlers
17731806
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17741807

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.