Skip to content

Instantly share code, notes, and snippets.

@ismail1432
Last active October 2, 2022 13:09
Show Gist options
  • Save ismail1432/9cf455f15ccc2d13effb77cf7f550bc3 to your computer and use it in GitHub Desktop.
Save ismail1432/9cf455f15ccc2d13effb77cf7f550bc3 to your computer and use it in GitHub Desktop.
<?php
// Message
class MyMessage
{
// boring stuff that you should adapt to your fit.
public $id = 42;
}
// Handler
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
// We should implements `MessageHandlerInterface` to register our handler
class MyMessageHandler implements MessageHandlerInterface
{
// Thanks to the type hint Symfony will match the message with the good handler
public function __invoke(MyMessage $message)
{
dump('We are in '.__CLASS__.' in method: '.__FUNCTION__, $message);
// return emojis for the fun.
return ['😍', '🎉', '🔥'];
}
}
// Controller
class HelloController extends AbstractController
{
#[Route('/hello', name: 'app_hello')]
public function hello(MessageBusInterface $bus): Response
{
// dispatch the message
$bus->dispatch(new MyMessage());
return $this->render('hello/index.html.twig', [
'value' => 'Hello',
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment