PHPackages                             esoul-cz/laravel-cqrs - 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. esoul-cz/laravel-cqrs

ActiveLibrary[Framework](/categories/framework)

esoul-cz/laravel-cqrs
=====================

Laravel simple CQRS implementation

v1.0.1(2mo ago)029gpl-2.0-or-laterPHPPHP &gt;=8.5

Since Apr 28Pushed 2mo agoCompare

[ Source](https://github.com/eSoul-cz/laravel-cqrs)[ Packagist](https://packagist.org/packages/esoul-cz/laravel-cqrs)[ RSS](/packages/esoul-cz-laravel-cqrs/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (8)Versions (3)Used By (0)

Laravel CQRS
============

[](#laravel-cqrs)

Laravel integration for [`esoul-cz/cqrs`](https://github.com/eSoul-cz/cqrs). The package wires the command bus and query bus into the Laravel container, supports handler auto-discovery, and adds artisan generators for commands, queries, and their handlers.

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

[](#requirements)

- PHP `>= 8.5`
- Laravel `10.x`, `11.x`, `12.x`, or `13.x`

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

[](#installation)

Install directly from Packagist:

```
composer require esoul-cz/laravel-cqrs
```

Or install directly from GitHub:

```
{
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/eSoul-cz/laravel-cqrs"
    }
  ]
}
```

Publish the configuration:

```
php artisan laravel-cqrs:install
```

That command publishes `config/cqrs.php`.

What the package registers
--------------------------

[](#what-the-package-registers)

The service provider registers these singletons in the Laravel container:

- `Esoul\Cqrs\Contracts\CommandBusInterface`
- `Esoul\Cqrs\Contracts\QueryBusInterface`

Both buses use Laravel's container to instantiate handlers, so constructor injection works out of the box.

The package also ships facade classes:

- `Esoul\LaravelCqrs\Facades\CommandBus`
- `Esoul\LaravelCqrs\Facades\QueryBus`

Default structure
-----------------

[](#default-structure)

The published config defaults to this application structure:

- commands: `app/Domain/CQRS/Command`
- queries: `app/Domain/CQRS/Query`
- command namespace: `App\Domain\CQRS\Command`
- query namespace: `App\Domain\CQRS\Query`

The generators and handler discovery read these values from `config/cqrs.php`, so you can move the classes by changing the configured namespaces and paths.

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

[](#configuration)

Published config lives in [`config/cqrs.php`](config/cqrs.php).

### Discovery

[](#discovery)

By default, handler discovery is enabled:

```
'discovery' => [
    'enabled' => true,
    'cache_dir' => storage_path('framework/cache/discovery'),
    'paths' => [
        'base' => [
            'base_namespace' => 'App\\Domain\\CQRS',
            'path' => app_path('Domain/CQRS'),
        ],
    ],
],
```

Each configured discovery path must define:

- `base_namespace`
- `path`

On boot, the package scans these paths for handlers marked with:

- `#[HandlesCommand(...)]`
- `#[HandlesQuery(...)]`

Discovered handlers are registered into the Laravel container and mapped into the corresponding bus.

### Manual registration

[](#manual-registration)

If you do not want discovery for some handlers, or you need explicit registration, use:

```
'commands' => [
    'register' => [
        App\Domain\CQRS\Command\CreateOrderCommand::class
            => App\Domain\CQRS\Command\CreateOrderCommandHandler::class,
    ],
],

'queries' => [
    'register' => [
        App\Domain\CQRS\Query\FindOrderQuery::class
            => App\Domain\CQRS\Query\FindOrderQueryHandler::class,
    ],
],
```

The package validates these mappings during boot:

- command classes must implement `CommandInterface`
- command handlers must implement `CommandHandlerInterface`
- query classes must implement `QueryInterface`
- query handlers must implement `QueryHandlerInterface`

Invalid mappings fail fast with an `InvalidArgumentException`.

Usage
-----

[](#usage)

### Dispatch a command

[](#dispatch-a-command)

```
