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 4 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
36 changes: 29 additions & 7 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ 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::

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

class SendNotification
{
private $content;

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

// ...getters
}

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

Expand Down Expand Up @@ -46,14 +67,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/SendNotificationHandler.php
namespace App\MessageHandler;

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

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

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

.. code-block:: xml
Expand All @@ -85,7 +107,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\SendNotificationHandler">
<tag name="messenger.message_handler" />
</service>
</services>
Expand All @@ -94,9 +116,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\SendNotificationHandler;

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

.. note::
Expand Down