Source: juststeveking-uk-astro.pages.dev

Laravel Command Bus
In Laravel 5.1 the Command Bus was replaced with Dispatchable Jobs, we can still use them but let us also look at how to add a Command Bus. To start using a command bus in Laravel, first we need an interface to bind to, because let’s be honest this is just good practice.

Commands are handled by CommandHandlers which is a little like the handle method on a Dispatchable Job in Laravel.

Our command bus will need to accept the Laravels bus Dispatcher to dispatch these commands nicely, so let us create app/CommandBus/Adapters/Laravel/LaravelCommandBus.php and make it look like the following: declare(strict_types=1); namespace App\CommandBus\Adapters\Laravel; use App\CommandBus\Contracts\CommandBusContract; use App\CommandBus\Contracts\CommandBusContract; use Illuminate\Bus\Dispatcher; final class LaravelCommandBus implements CommandBusContract { public function __construct() { private Dispatcher $bus, } public function dispatch(CommandContract $command): mixed { return $this->bus->dispatch($command); } public function map(array $map): void { $this->bus->map($map); }}

Next we should look at how we want to handle this command itself, before we worry about implementing this command bus.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners