@@ -50,25 +50,37 @@ serialized::
5050
5151.. _messenger-handler :
5252
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+
5358A 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)::
5762
5863 // src/MessageHandler/SmsNotificationHandler.php
5964 namespace App\MessageHandler;
6065
6166 use App\Message\SmsNotification;
62- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
67+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
6368
64- class SmsNotificationHandler implements MessageHandlerInterface
69+ #[AsMessageHandler]
70+ class SmsNotificationHandler
6571 {
6672 public function __invoke(SmsNotification $message)
6773 {
6874 // ... do some work - like sending an SMS message!
6975 }
7076 }
7177
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+
7284Thanks to :ref: `autoconfiguration <services-autoconfigure >` and the ``SmsNotification ``
7385type-hint, Symfony knows that this handler should be called when an ``SmsNotification ``
7486message 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::
349361
350362 use App\Message\NewUserWelcomeEmail;
351363 use App\Repository\UserRepository;
352- use Symfony\Component\Messenger\Handler\MessageHandlerInterface ;
364+ use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
353365
354- class NewUserWelcomeEmailHandler implements MessageHandlerInterface
366+ #[AsMessageHandler]
367+ class NewUserWelcomeEmailHandler
355368 {
356369 private $userRepository;
357370
@@ -1784,6 +1797,40 @@ on a case-by-case basis via the :class:`Symfony\\Component\\Messenger\\Stamp\\Se
17841797Customizing Handlers
17851798--------------------
17861799
1800+ Configuring Handlers Using Attributes
1801+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1802+
1803+ .. versionadded :: 5.4
1804+
1805+ The ``#[AsMessageHandler] `` PHP attribute was introduced in Symfony
1806+ 5.4. PHP attributes require at least PHP 8.0.
1807+
1808+ You can configure your handler by passing options to the attribute::
1809+
1810+ // src/MessageHandler/SmsNotificationHandler.php
1811+ namespace App\MessageHandler;
1812+
1813+ use App\Message\OtherSmsNotification;
1814+ use App\Message\SmsNotification;
1815+ use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1816+
1817+ #[AsMessageHandler(fromTransport: 'async', priority: 10)]
1818+ class SmsNotificationHandler
1819+ {
1820+ public function __invoke(SmsNotification $message)
1821+ {
1822+ // ...
1823+ }
1824+ }
1825+
1826+ Possible options to configure with the attribute are:
1827+
1828+ * ``bus ``
1829+ * ``fromTransport ``
1830+ * ``handles ``
1831+ * ``method ``
1832+ * ``priority ``
1833+
17871834.. _messenger-handler-config :
17881835
17891836Manually Configuring Handlers
0 commit comments