@@ -50,25 +50,37 @@ serialized::
50
50
51
51
.. _messenger-handler :
52
52
53
+ .. versionadded :: 5.4
54
+
55
+ The ``#[AsMessageHandler] `` PHP attribute was introduced in Symfony
56
+ 5.4. PHP attributes require at least PHP 8.0.
57
+
53
58
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)::
59
+ create a class that has the :class: `Symfony\\ Component\\ Messenger\\ Attribute \\ AsMessageHandler `
60
+ attribute and has an ``__invoke() `` method that's type-hinted with the
61
+ message class (or a message interface)::
57
62
58
63
// src/MessageHandler/SmsNotificationHandler.php
59
64
namespace App\MessageHandler;
60
65
61
66
use App\Message\SmsNotification;
62
- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
67
+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
63
68
64
- class SmsNotificationHandler implements MessageHandlerInterface
69
+ #[AsMessageHandler]
70
+ class SmsNotificationHandler
65
71
{
66
72
public function __invoke(SmsNotification $message)
67
73
{
68
74
// ... do some work - like sending an SMS message!
69
75
}
70
76
}
71
77
78
+ .. note ::
79
+
80
+ You can also create a class without the attribute (e.g. if you're
81
+ using PHP 7.4), by implementing :class: `Symfony\\ Component\\ Messenger\\ Handler\\ MessageHandlerInterface `
82
+ instead.
83
+
72
84
Thanks to :ref: `autoconfiguration <services-autoconfigure >` and the ``SmsNotification ``
73
85
type-hint, Symfony knows that this handler should be called when an ``SmsNotification ``
74
86
message is dispatched. Most of the time, this is all you need to do. But you can
@@ -349,9 +361,10 @@ Then, in your handler, you can query for a fresh object::
349
361
350
362
use App\Message\NewUserWelcomeEmail;
351
363
use App\Repository\UserRepository;
352
- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
364
+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
353
365
354
- class NewUserWelcomeEmailHandler implements MessageHandlerInterface
366
+ #[AsMessageHandler]
367
+ class NewUserWelcomeEmailHandler
355
368
{
356
369
private $userRepository;
357
370
@@ -1673,6 +1686,40 @@ on a case-by-case basis via the :class:`Symfony\\Component\\Messenger\\Stamp\\Se
1673
1686
Customizing Handlers
1674
1687
--------------------
1675
1688
1689
+ Configuring Handlers Using Attributes
1690
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1691
+
1692
+ .. versionadded :: 5.4
1693
+
1694
+ The ``#[AsMessageHandler] `` PHP attribute was introduced in Symfony
1695
+ 5.4. PHP attributes require at least PHP 8.0.
1696
+
1697
+ You can configure your handler by passing options to the attribute::
1698
+
1699
+ // src/MessageHandler/SmsNotificationHandler.php
1700
+ namespace App\MessageHandler;
1701
+
1702
+ use App\Message\OtherSmsNotification;
1703
+ use App\Message\SmsNotification;
1704
+ use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1705
+
1706
+ #[AsMessageHandler(fromTransport: 'async', priority: 10)]
1707
+ class SmsNotificationHandler
1708
+ {
1709
+ public function __invoke(SmsNotification $message)
1710
+ {
1711
+ // ...
1712
+ }
1713
+ }
1714
+
1715
+ Possible options to configure with the attribute are:
1716
+
1717
+ * ``bus ``
1718
+ * ``fromTransport ``
1719
+ * ``handles ``
1720
+ * ``method ``
1721
+ * ``priority ``
1722
+
1676
1723
.. _messenger-handler-config :
1677
1724
1678
1725
Manually Configuring Handlers
0 commit comments