ignaciocastro0713/cqbus-mediator
Composer 安装命令:
composer require ignaciocastro0713/cqbus-mediator
包简介
A modern CQRS Mediator for Laravel using PHP 8 Attributes, auto-discovery, and routing pipelines.
README 文档
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.
ignaciocastro0713/cqbus-mediator 适用场景与选型建议
ignaciocastro0713/cqbus-mediator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「attributes」 「laravel」 「action」 「event-bus」 「cqrs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ignaciocastro0713/cqbus-mediator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ignaciocastro0713/cqbus-mediator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ignaciocastro0713/cqbus-mediator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Write down your routing mapping at one place
A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
The last validation library you will ever need!
A PHPDoc docblock interpreter that is simple, easy to use, and provides Attribute alternatives.
统计信息
- 总下载量: 91
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-21