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 7866a09

Browse filesBrowse files
committed
Merge branch '6.0' into 6.1
* 6.0: Removed 5.4 versionadded [#15990] Minor changes [Messenger] Autoconfigurable attributes
2 parents 11e5c81 + 2e72365 commit 7866a09
Copy full SHA for 7866a09

File tree

1 file changed

+44
-7
lines changed
Filter options

1 file changed

+44
-7
lines changed

‎messenger.rst

Copy file name to clipboardExpand all lines: messenger.rst
+44-7Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,31 @@ 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 that has the :class:`Symfony\\Component\\Messenger\\Attribute\\AsMessageHandler`
55+
attribute and has an ``__invoke()`` method that's type-hinted with the
56+
message class (or a message interface)::
5757

5858
// src/MessageHandler/SmsNotificationHandler.php
5959
namespace App\MessageHandler;
6060

6161
use App\Message\SmsNotification;
62-
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
62+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
6363

64-
class SmsNotificationHandler implements MessageHandlerInterface
64+
#[AsMessageHandler]
65+
class SmsNotificationHandler
6566
{
6667
public function __invoke(SmsNotification $message)
6768
{
6869
// ... do some work - like sending an SMS message!
6970
}
7071
}
7172

73+
.. note::
74+
75+
You can also create a class without the attribute (e.g. if you're
76+
using PHP 7.4), by implementing :class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface`
77+
instead.
78+
7279
Thanks to :ref:`autoconfiguration <services-autoconfigure>` and the ``SmsNotification``
7380
type-hint, Symfony knows that this handler should be called when an ``SmsNotification``
7481
message is dispatched. Most of the time, this is all you need to do. But you can
@@ -349,9 +356,10 @@ Then, in your handler, you can query for a fresh object::
349356

350357
use App\Message\NewUserWelcomeEmail;
351358
use App\Repository\UserRepository;
352-
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
359+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
353360

354-
class NewUserWelcomeEmailHandler implements MessageHandlerInterface
361+
#[AsMessageHandler]
362+
class NewUserWelcomeEmailHandler
355363
{
356364
private $userRepository;
357365

@@ -1673,6 +1681,35 @@ on a case-by-case basis via the :class:`Symfony\\Component\\Messenger\\Stamp\\Se
16731681
Customizing Handlers
16741682
--------------------
16751683

1684+
Configuring Handlers Using Attributes
1685+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1686+
1687+
You can configure your handler by passing options to the attribute::
1688+
1689+
// src/MessageHandler/SmsNotificationHandler.php
1690+
namespace App\MessageHandler;
1691+
1692+
use App\Message\OtherSmsNotification;
1693+
use App\Message\SmsNotification;
1694+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1695+
1696+
#[AsMessageHandler(fromTransport: 'async', priority: 10)]
1697+
class SmsNotificationHandler
1698+
{
1699+
public function __invoke(SmsNotification $message)
1700+
{
1701+
// ...
1702+
}
1703+
}
1704+
1705+
Possible options to configure with the attribute are:
1706+
1707+
* ``bus``
1708+
* ``fromTransport``
1709+
* ``handles``
1710+
* ``method``
1711+
* ``priority``
1712+
16761713
.. _messenger-handler-config:
16771714

16781715
Manually Configuring Handlers

0 commit comments

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