Symfony 5 Messenger not auto configuring message handler

  symfony, symfony-messenger, symfony5

I am new to Symfony framework and am trying to set up a project with Messenger (https://symfony.com/doc/current/messenger.html). My understanding is after setting up the handler class with “implements MessageHandlerInterface” the handler should be available for use but it does not seem to be working for me. I have tried several different things including setting up a new project from scratch. I am using Symfony 5.0.4.

I set up the project like this:

symfony new --full testMessenger
composer require messenger
php bin/console make:controller

Then I made a new messenger and handler and called it from the controller. Doing that I get the exception: “No handler for message “AppMessageMessage”.

php bin/console debug:messenger

Output:

Messenger 
========= 

messenger.bus.default 
--------------------- 

The following messages can be dispatched: 

-----------------------------------------------------  
 SymfonyComponentMailerMessengerSendEmailMessage   
     handled by mailer.messenger.message_handler       
 SymfonyComponentNotifierMessageChatMessage        
     handled by chatter.messenger.chat_handler         
 SymfonyComponentNotifierMessageSmsMessage         
     handled by texter.messenger.sms_handler           
-----------------------------------------------------  

My code is basically the same as the samples in the message handler documentation, but I will add it here.

// src/Message.php
<?php

namespace AppMessage;

class Message
{
    private $content;

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

    public function getContent(): string
    {
        return $this->content;
    }
}


// src/MessageHandler.php
<?php

namespace AppMessageHandler;

use AppMessageMessage;
use SymfonyComponentMessengerHandlerMessageHandlerInterface;

class SmsNotificationHandler implements MessageHandlerInterface
{
    public function __invoke(Message $message)
    {
        // ... do some work - like sending an SMS message!
    }
}


// src/Controller/MessageController.php
<?php

namespace AppController;

use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentRoutingAnnotationRoute;
use AppMessageMessage;

class MessageController extends AbstractController
{
    /**
     * @Route("/message", name="message")
     */
    public function index()
    {
        $this->dispatchMessage(new Message('Look! I created a message!'));

        return $this->render('message/index.html.twig', [
            'controller_name' => 'MessageController',
        ]);
    }
}

Source: Symfony Questions

LEAVE A COMMENT