illuminate/bus
Illuminate Bus provides Laravel’s command bus for dispatching jobs, commands, and queued tasks. It supports sync and async dispatch, job chaining, batching, middleware-style pipelines, and robust integration with the queue system for background processing.
Start by verifying illuminate/bus is included in your Laravel installation (it’s auto-registered via app.php or service provider discovery in Laravel 11+). No extra installation is needed—just begin dispatching commands. Your first real-world use case is replacing inline business logic (e.g., in a controller) with a dedicated command:
// Instead of:
$order->process();
// Use:
dispatch(new ProcessOrder($order));
Dispatch synchronously in tests or inline contexts via dispatchSync(), and asynchronously (via queues) via dispatch(). Check Bus::dispatched() assertions in feature tests to validate command intent.
handle() directly inside your command class for simplicity (e.g., ProcessOrder::handle()), especially for small/medium domains.Bus::pipeThrough([LogCommands::class])) or per-command via Bus::dispatchSync($command)->then(...) (advanced).ShouldQueue to automatically dispatch to your queue connection—no extra config needed. Use onQueue(), onConnection(), and chain() for orchestration.Bus facade to assert dispatched commands without side effects. In feature tests, use Bus::fake() to prevent real dispatching while verifying expectations.App/Commands and handlers (if separated) in App/Handlers/Commands, mirroring DDD principles to decouple business rules from HTTP layer concerns.GenerateInvoiceHandler for GenerateInvoice). Rename mis-matched handlers to avoid silent failures.handle() Methods: Parameters must match exact types (e.g., Order|null $order won’t auto-resolve if null isn’t accepted); use strict type declarations and validation.Bus from the container directly—prefer dependency injection in classes or use the Bus facade for clarity.illuminate/* ^13.0; mixing with older Laravel versions (e.g., 10.x) will cause dependency conflicts. Always align with your Laravel version.ShouldQueue is implemented, queue workers are running (php artisan queue:work), and exceptions are caught/logged in the job.Bus (e.g., CustomBus extends Bus) and bind it in AppServiceProvider to inject custom logic (e.g., correlation IDs, audit logging) without monkey-patching core behavior.How can I help you explore Laravel packages today?