composer require symfony/messenger
Create a message class that represents the data you want to pass through the queue. For example, you could create a SendEmailMessage
class with properties like to
, subject
, and body
.
Create a handler class that will process the message when it is received from the queue. This class should implement MessageHandlerInterface
and have a __invoke
method to handle the message. For example, you could create a SendEmailHandler
class with a __invoke(SendEmailMessage $message)
method that sends the email.
Configure the Messenger component in your services.yaml
file to map your message class to its respective handler class. For example:
services:
App\Message\SendEmailHandler: ~
messenger.transport_factory.default:
class: 'Symfony\Component\Messenger\Transport\Sync\SyncTransportFactory'
messenger.transport.default:
dsn: 'sync://default'
dispatch
method of the MessageBusInterface
service to send a message to the queue. For example:$this->messageBus->dispatch(new SendEmailMessage('[email protected]', 'Hello', 'This is a test email'));
php bin/console messenger:consume