Skip to content

Commit ae09128

Browse files
committed
minor #10624 [Messenger] Improve the Messenger Handler Example (arkste)
This PR was squashed before being merged into the 4.1 branch (closes #10624). Discussion ---------- [Messenger] Improve the Messenger Handler Example If you follow the Examples on the Messenger Documentation, its confusing having a `SendNotification`-Message first and then a handler implementing `MyMessage`. This PR addresses this issue and changes the Handler-Example to match the Message-Dispatch-Example. Commits ------- 9a36a75 [Messenger] Improve the Messenger Handler Example
2 parents 1e8d8ab + 9a36a75 commit ae09128

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

messenger.rst

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ install messenger before using it:
1919
2020
$ composer require messenger
2121
22+
Message
23+
-------
24+
25+
Before you can send a message, you must create it first. There is no specific
26+
requirement for a message, except it should be serializable and unserializable
27+
by a Symfony Serializer instance::
28+
29+
// src/Message/SmsNotification.php
30+
namespace App\Message;
31+
32+
class SmsNotification
33+
{
34+
private $content;
35+
36+
public function __construct(string $content)
37+
{
38+
$this->content = $content;
39+
}
40+
41+
// ...getters
42+
}
43+
2244
Using the Messenger Service
2345
---------------------------
2446

@@ -28,15 +50,15 @@ you need it, like in a controller::
2850
// src/Controller/DefaultController.php
2951
namespace App\Controller;
3052

31-
use App\Message\SendNotification;
53+
use App\Message\SmsNotification;
3254
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
3355
use Symfony\Component\Messenger\MessageBusInterface;
3456

3557
class DefaultController extends AbstractController
3658
{
3759
public function index(MessageBusInterface $bus)
3860
{
39-
$bus->dispatch(new SendNotification('A string to be sent...'));
61+
$bus->dispatch(new SmsNotification('A string to be sent...'));
4062
}
4163
}
4264

@@ -46,14 +68,15 @@ Registering Handlers
4668
In order to do something when your message is dispatched, you need to create a
4769
message handler. It's a class with an ``__invoke`` method::
4870

49-
// src/MessageHandler/MyMessageHandler.php
71+
// src/MessageHandler/SmsNotificationHandler.php
5072
namespace App\MessageHandler;
5173

74+
use App\Message\SmsNotification;
5275
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
5376

54-
class MyMessageHandler implements MessageHandlerInterface
77+
class SmsNotificationHandler implements MessageHandlerInterface
5578
{
56-
public function __invoke(MyMessage $message)
79+
public function __invoke(SmsNotification $message)
5780
{
5881
// do something with it.
5982
}
@@ -72,7 +95,7 @@ If you're not using service autoconfiguration, then you need to add this config:
7295
7396
# config/services.yaml
7497
services:
75-
App\MessageHandler\MyMessageHandler:
98+
App\MessageHandler\SmsNotificationHandler:
7699
tags: [messenger.message_handler]
77100
78101
.. code-block:: xml
@@ -85,7 +108,7 @@ If you're not using service autoconfiguration, then you need to add this config:
85108
http://symfony.com/schema/dic/services/services-1.0.xsd">
86109
87110
<services>
88-
<service id="App\MessageHandler\MyMessageHandler">
111+
<service id="App\MessageHandler\SmsNotificationHandler">
89112
<tag name="messenger.message_handler" />
90113
</service>
91114
</services>
@@ -94,9 +117,9 @@ If you're not using service autoconfiguration, then you need to add this config:
94117
.. code-block:: php
95118
96119
// config/services.php
97-
use App\MessageHandler\MyMessageHandler;
120+
use App\MessageHandler\SmsNotificationHandler;
98121
99-
$container->register(MyMessageHandler::class)
122+
$container->register(SmsNotificationHandler::class)
100123
->addTag('messenger.message_handler');
101124
102125
.. note::

0 commit comments

Comments
 (0)