@@ -51,24 +51,31 @@ serialized::
51
51
.. _messenger-handler :
52
52
53
53
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)::
57
57
58
58
// src/MessageHandler/SmsNotificationHandler.php
59
59
namespace App\MessageHandler;
60
60
61
61
use App\Message\SmsNotification;
62
- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
62
+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
63
63
64
- class SmsNotificationHandler implements MessageHandlerInterface
64
+ #[AsMessageHandler]
65
+ class SmsNotificationHandler
65
66
{
66
67
public function __invoke(SmsNotification $message)
67
68
{
68
69
// ... do some work - like sending an SMS message!
69
70
}
70
71
}
71
72
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
+
72
79
Thanks to :ref: `autoconfiguration <services-autoconfigure >` and the ``SmsNotification ``
73
80
type-hint, Symfony knows that this handler should be called when an ``SmsNotification ``
74
81
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::
349
356
350
357
use App\Message\NewUserWelcomeEmail;
351
358
use App\Repository\UserRepository;
352
- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
359
+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
353
360
354
- class NewUserWelcomeEmailHandler implements MessageHandlerInterface
361
+ #[AsMessageHandler]
362
+ class NewUserWelcomeEmailHandler
355
363
{
356
364
private $userRepository;
357
365
@@ -1673,6 +1681,35 @@ on a case-by-case basis via the :class:`Symfony\\Component\\Messenger\\Stamp\\Se
1673
1681
Customizing Handlers
1674
1682
--------------------
1675
1683
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
+
1676
1713
.. _messenger-handler-config :
1677
1714
1678
1715
Manually Configuring Handlers
0 commit comments