Getting Started
A lightweight, extensible message bus implementation for Laravel supporting both Command and Query patterns with middleware pipeline architecture.
Requirements
Section titled “Requirements”- PHP 8.5+
- Laravel 12.28+
Installation
Section titled “Installation”composer require cline/message-busThe service provider is auto-discovered. Handler discovery works automatically in local development.
Quick Start
Section titled “Quick Start”Command Bus
Section titled “Command Bus”Dispatch commands for write operations:
use Cline\MessageBus\Facades\CommandBus;
$result = CommandBus::dispatch(new CreateUserCommand( email: 'user@example.com', name: 'John Doe',));Query Bus
Section titled “Query Bus”Execute queries for read operations:
use Cline\MessageBus\Facades\QueryBus;
$user = QueryBus::ask(new GetUserByEmailQuery( email: 'user@example.com',));Core Concepts
Section titled “Core Concepts”CQRS Pattern
Section titled “CQRS Pattern”This package implements Command Query Responsibility Segregation (CQRS):
- Commands - Write operations that modify state
- Queries - Read operations that return data
Separating reads and writes allows you to optimize each path independently.
Pipeline Architecture
Section titled “Pipeline Architecture”Both buses use Laravel’s Pipeline to process messages through middleware:
Command → Middleware 1 → Middleware 2 → Handler → ResultConfigure middleware globally via config or per-dispatch with withMiddleware().
Automatic Handler Discovery
Section titled “Automatic Handler Discovery”In local development, handlers are discovered automatically via PHP attributes:
use Cline\MessageBus\Commands\Attributes\AsCommandHandler;
#[AsCommandHandler(CreateUserCommand::class)]final readonly class CreateUserHandler{ public function handle(CreateUserCommand $command): User { // Handle the command }}For production, cache the handler map for performance.
Configuration
Section titled “Configuration”Publish the config file:
php artisan vendor:publish --tag=message-bus-configConfig options:
return [ 'paths' => [ 'command_handlers' => base_path('bootstrap/cache/command-handlers.php'), 'query_handlers' => base_path('bootstrap/cache/query-handlers.php'), ],];Handler Directory Conventions
Section titled “Handler Directory Conventions”The discovery system scans these directory patterns:
- Legacy:
Application/CommandHandler/andApplication/QueryHandler/ - Modern:
Application/Command/Handlers/andApplication/Query/Handlers/
All handler classes must be within the Monolith\ namespace.