ignaciocastro0713 / cqbus-mediator
A modern CQRS Mediator for Laravel using PHP 8 Attributes, auto-discovery, and routing pipelines.
Package info
github.com/IgnacioCastro0713/cqbus-mediator
pkg:composer/ignaciocastro0713/cqbus-mediator
Requires
- php: ^8.2
- illuminate/container: ^11.0 || ^12.0 || ^13.0
- illuminate/contracts: ^11.0 || ^12.0 || ^13.0
- illuminate/filesystem: ^11.0 || ^12.0 || ^13.0
- illuminate/pipeline: ^11.0 || ^12.0 || ^13.0
- illuminate/support: ^11.0 || ^12.0 || ^13.0
- spatie/php-structure-discoverer: ^2.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.84
- mockery/mockery: ^1.6
- orchestra/testbench: ^9.0 || ^10.0 || ^11.0
- pestphp/pest: ^2.11|^3.0|^3.8
- phpbench/phpbench: ^1.4
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.5|^10.0|^11.5.3
- dev-main
- v7.0.1
- v7.0.0
- v6.2.0
- v6.1.0
- v6.0.1
- v6.0.0
- v5.4.1
- v5.4.0
- v5.3.0
- v5.2.0
- v5.1.0
- v5.0.0
- v4.1.0
- v4.0.1
- v4.0.0
- v3.1.0
- v3.0.0
- v2.4.1
- v2.4.0
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.1
- v1.0.0
- dev-claude/discovery-library-alternatives-956w2
This package is auto-updated.
Last update: 2026-04-23 05:08:36 UTC
README
A zero-configuration Command/Query Bus for Laravel
Decouple your controllers from business logic using the Mediator pattern (CQRS) and PHP 8 Attributes โ with zero boilerplate.
The Problem
Controllers that mix HTTP, business logic, and side effects are hard to test and maintain.
| โ Before โ everything tangled in one place | โ After โ each concern in its own place |
|---|---|
class UserController extends Controller { public function register(Request $request) { $request->validate([ 'email' => 'required|email', 'password' => 'required', ]); DB::beginTransaction(); try { $user = User::create($request->all()); Mail::to($user)->send(new WelcomeEmail()); Log::info("User registered"); DB::commit(); return response()->json($user, 201); } catch (\Exception $e) { DB::rollBack(); throw $e; } } } |
#[Api] class RegisterUserAction { use AsAction; public function __construct( private readonly Mediator $mediator ) {} public static function route(Router $router): void { $router->post('/register'); } public function handle(RegisterUserRequest $request): JsonResponse { // Handler runs the logic $user = $this->mediator->send($request); // Notifications handle side effects $this->mediator->publish(new UserRegisteredEvent($user)); return response()->json($user, 201); } } |
Features
|
โก Zero Config
Handlers are auto-discovered via |
๐ข Event Bus Publish events to multiple notification handlers, with priority control over execution order. |
|
๐ฎ Attribute Routing
Define routes directly on Action classes with |
๐ Pipelines
Apply middleware-like logic (transactions, logging) globally or per-handler via |
|
๐งช Testing Fakes Assert dispatched requests without executing business logic โ built-in, no setup required. |
๐ Production Cache
Eliminate discovery overhead (~2,500x faster boot) with |
Installation
composer require ignaciocastro0713/cqbus-mediator
The package is auto-discovered. Optionally publish the config file:
php artisan vendor:publish --tag=mediator-config
Quick Start
The package supports two patterns:
| Pattern | Method | Direction | Use for |
|---|---|---|---|
| Command / Query | send() |
1-to-1 | Business logic that reads or writes |
| Event Bus | publish() |
1-to-N | Side effects (emails, logs, etc.) |
1. Scaffold your classes
php artisan make:mediator-handler RegisterUserHandler --action
Generates RegisterUserRequest, RegisterUserHandler, and RegisterUserAction in one go.
2. Write your Handler
#[RequestHandler(RegisterUserRequest::class)] class RegisterUserHandler { public function handle(RegisterUserRequest $request): User { return User::create($request->validated()); } }
3. Dispatch from your Action
$user = $this->mediator->send($request);
The Mediator discovers and routes to the correct handler automatically.
Documentation
| ๐ฆ Installation | ๐ง Core Concepts | โก Commands & Queries |
| ๐ข Event Bus | ๐ฎ Routing & Actions | ๐ Pipelines |
| ๐งช Testing | ๐ Console Commands | ๐ Production & Performance |
Requirements
- PHP 8.2+
- Laravel 11.0+
Contributing
Feel free to open issues or submit pull requests on the GitHub repository.
License
This package is open-sourced software licensed under the MIT license.