This page covers handler definitions by message type, callable formats, pitfalls, and valid handler signatures.
If you haven't read Message handler: simple setup yet, start there — it introduces handler classes and the zero-config FQCN approach. For a conceptual overview of what messages and handlers are, see Messages and handlers: concepts.
Handler definitions are configured in:
$params['yiisoft/queue']['handlers']when using yiisoft/config, or- the
$handlersargument ofYiisoft\Queue\Worker\Workerwhen creating it manually.
Use a short stable message type when pushing a Message instead of a PHP class name:
use Yiisoft\Queue\Message\Message;
new Message('send-email', ['data' => '...']); // "send-email" is the message type hereConfig:
Map message type to a handler in $params:
return [
'yiisoft/queue' => [
'handlers' => [
'send-email' => /** handler definition */,
],
],
];Handler definition should be either an extended callable definition or a container identifier that resolves to a MessageHandlerInterface instance.
While FQCN-as-name is convenient inside a single application, mapping by a short name is often a better contract. That is true when messages are produced outside the current codebase, or when you want to create a stable public API for inter-service communication.
Typical cases:
- Another application pushes messages to the same broker.
- A different language/runtime produces messages.
In these cases you typically keep message types small and stable, and map them in config:
return [
'yiisoft/queue' => [
'handlers' => [
'file-download' => \App\Queue\RemoteFileHandler::class,
],
],
];This way external producers never need to know your internal PHP class names.
- A PHP class name that is not registered in the DI container will not be auto-instantiated.
- yiisoft/definitions array format (like
['class' => ..., '__construct()' => ...]) is not supported for handlers.
The worker recognises three callable signatures:
MessageHandlerInterface— implement the interface; the worker callshandle(MessageInterface $message): voiddirectly (covered in Message handler).- Invokable class — add
__invoke(MessageInterface $message): void. - Explicit method — reference as
[HandlerClass::class, 'handle']withhandle(MessageInterface $message): voidas the entry point.
When using yiisoft/config, configure handlers under the yiisoft/queue params key:
return [
'yiisoft/queue' => [
'handlers' => [
'message-type' => [FooHandler::class, 'handle'],
],
],
];This config is consumed by the DI definitions from config/di.php where the Worker is constructed with $params['yiisoft/queue']['handlers'].