Skip to content

[Messenger] Improve the Messenger Handler Example #10624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ install messenger before using it:

$ composer require messenger

Message
-------

Before you can send a message, you must create it first. There is no specific
requirement for a message, except it should be serializable and unserializable
by a Symfony Serializer instance::

// src/Message/SmsNotification.php
namespace App\Message;

class SmsNotification
{
private $content;

public function __construct(string $content)
{
$this->content = $content;
}

// ...getters
}

Using the Messenger Service
---------------------------

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

use App\Message\SendNotification;
use App\Message\SmsNotification;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Messenger\MessageBusInterface;

class DefaultController extends AbstractController
{
public function index(MessageBusInterface $bus)
{
$bus->dispatch(new SendNotification('A string to be sent...'));
$bus->dispatch(new SmsNotification('A string to be sent...'));
}
}

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

// src/MessageHandler/MyMessageHandler.php
// src/MessageHandler/SmsNotificationHandler.php
namespace App\MessageHandler;

use App\Message\SmsNotification;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class MyMessageHandler implements MessageHandlerInterface
class SmsNotificationHandler implements MessageHandlerInterface
{
public function __invoke(MyMessage $message)
public function __invoke(SmsNotification $message)
{
// do something with it.
}
Expand All @@ -72,7 +95,7 @@ If you're not using service autoconfiguration, then you need to add this config:

# config/services.yaml
services:
App\MessageHandler\MyMessageHandler:
App\MessageHandler\SmsNotificationHandler:
tags: [messenger.message_handler]

.. code-block:: xml
Expand All @@ -85,7 +108,7 @@ If you're not using service autoconfiguration, then you need to add this config:
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="App\MessageHandler\MyMessageHandler">
<service id="App\MessageHandler\SmsNotificationHandler">
<tag name="messenger.message_handler" />
</service>
</services>
Expand All @@ -94,9 +117,9 @@ If you're not using service autoconfiguration, then you need to add this config:
.. code-block:: php

// config/services.php
use App\MessageHandler\MyMessageHandler;
use App\MessageHandler\SmsNotificationHandler;

$container->register(MyMessageHandler::class)
$container->register(SmsNotificationHandler::class)
->addTag('messenger.message_handler');

.. note::
Expand Down