PHPackages                             different-universe/laravel-cqbus-mediator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. different-universe/laravel-cqbus-mediator

ActiveLibrary[Framework](/categories/framework)

different-universe/laravel-cqbus-mediator
=========================================

Lightweight, modern CQS Mediator for Laravel using PHP 8 attributes and auto-discovery.

v1.0.0(2mo ago)06MITPHPPHP ^8.2

Since May 5Pushed 2mo agoCompare

[ Source](https://github.com/different-universe/laravel-cqbus-mediator)[ Packagist](https://packagist.org/packages/different-universe/laravel-cqbus-mediator)[ Docs](https://github.com/different-universe/laravel-cqbus-mediator)[ RSS](/packages/different-universe-laravel-cqbus-mediator/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (11)Versions (2)Used By (0)

Laravel CQBus Mediator
======================

[](#laravel-cqbus-mediator)

Lightweight CQS/CQRS-style mediator for Laravel applications. The package lets you describe application use cases as small command, query, and internal handler classes, route messages to handlers automatically via PHP attributes, and wrap execution with Laravel pipelines.

Instead of calling large service classes from controllers, you dispatch an explicit message:

```
use App\Mediator\Orders\CreateOrder;
use DifferentUniverse\CqbusMediator\Contracts\Mediator;

final class OrderController
{
    public function __construct(
        private readonly Mediator $mediator,
    ) {
    }

    public function store(StoreOrderRequest $request): JsonResponse
    {
        $result = $this->mediator->send(new CreateOrder(
            userId: $request->user()->id,
            items: $request->validated('items'),
        ));

        return response()->json($result, 201);
    }
}
```

Motivation
----------

[](#motivation)

Controllers are usually a poor place for business logic. They should deal with the HTTP layer: request data, validation, authentication context, response shape, status codes, redirects, headers, and framework-specific concerns.

Business operations are easier to understand and test when they are modeled as separate application units. A `CreateOrderHandler`, `RefundPaymentHandler`, or `LookupCustomerBalanceHandler` represents one concrete use case that can be reused from controllers, console commands, queue jobs, event listeners, tests, or any other entry point.

The common "fat controller" problem appears when HTTP details, application flow, persistence, domain rules, authorization checks, logging, and side effects grow together in the same method. The result is hard to test, hard to reuse outside HTTP, and hard to change without pulling on hidden dependencies.

Classic service classes help, but they often drift into broad buckets such as `OrderService`, `UserService`, or `PaymentService`. Over time they collect many unrelated methods, their names stop describing a single operation, and cross-cutting behavior such as transactions, tracing, tenant context, or query caching becomes inconsistent.

This package pushes the code toward smaller use-case handlers:

- one message maps to one command/query handler;
- handlers are discovered automatically from configured paths;
- command and query flows can have different configured pipelines;
- local pipelines can be declared directly on handlers;
- internal handlers can build explicit post-processing chains after the main handler returns.

Features
--------

[](#features)

- Command, query, and internal handlers via PHP attributes.
- Automatic handler discovery from configured paths.
- Message class routing and explicit string channels.
- Laravel container resolution for handlers and pipelines.
- Laravel `Illuminate\Pipeline\Pipeline` integration.
- Global, command-specific, query-specific, and local handler pipelines.
- Internal post-processing handler chains through output channels.
- Cache mode for production.
- Artisan generators for handler classes.
- Laravel package auto-discovery and facade support.
- Laravel Octane friendly mediator singleton.

Requirements
------------

[](#requirements)

- PHP `^8.2`
- Laravel `^11.0`, `^12.0`, or `^13.0`

Installation
------------

[](#installation)

Install the package:

```
composer require different-universe/laravel-cqbus-mediator
```

Publish the config file:

```
php artisan vendor:publish --tag=mediator-config
```

Optionally publish stubs for generator customization:

```
php artisan vendor:publish --tag=mediator-stubs
```

The service provider is registered automatically through Laravel package discovery.

If package discovery is disabled, register the provider manually:

```
// bootstrap/providers.php

return [
    App\Providers\AppServiceProvider::class,
    DifferentUniverse\CqbusMediator\MediatorServiceProvider::class,
];
```

Configuration
-------------

[](#configuration)

After publishing, the config is available at `config/mediator.php`:

```
