PHPackages                             mpirogov/yii2-command-bus2 - 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. [Queues &amp; Workers](/categories/queues)
4. /
5. mpirogov/yii2-command-bus2

ActiveYii2-extension[Queues &amp; Workers](/categories/queues)

mpirogov/yii2-command-bus2
==========================

Yii2 Command Bus extension

1.1(8y ago)054BSD-3-ClausePHP

Since Jan 20Pushed 8y ago1 watchersCompare

[ Source](https://github.com/mpirogov/yii2-command-bus2)[ Packagist](https://packagist.org/packages/mpirogov/yii2-command-bus2)[ RSS](/packages/mpirogov-yii2-command-bus2/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (6)Versions (3)Used By (0)

Yii2 Command Bus
================

[](#yii2-command-bus)

Command Bus for Yii2

[![Build Status](https://camo.githubusercontent.com/17886b2ba072cfad65cde2810d642e59b8c0756109c693a0383dbb5d5633dba3/68747470733a2f2f7472617669732d63692e6f72672f6d7069726f676f762f796969322d636f6d6d616e642d627573322e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mpirogov/yii2-command-bus2)

What is Command Bus?
--------------------

[](#what-is-command-bus)

> The idea of a command bus is that you create command objects that represent what you want your application to do. Then, you toss it into the bus and the bus makes sure that the command object gets to where it needs to go. So, the command goes in -&gt; the bus hands it off to a handler -&gt; and then the handler actually does the job. The command essentially represents a method call to your service layer.

[Shawn McCool ©](http://shawnmc.cool/command-bus)

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist mpirogov/yii2-command-bus2

```

or add

```
"mpirogov/yii2-command-bus2": "^1.0"

```

to your composer.json file

Setting Up
----------

[](#setting-up)

### 1. Command Bus Service

[](#1-command-bus-service)

After the installation, first step is to set the command bus component.

```
return [
    // ...
    'components' => [
        'commandBus' => [
            'class' => 'mpirogov\bus\CommandBus'
        ]
    ],
];
```

### 2. Background commands support (optional)

[](#2-background-commands-support-optional)

Install required package:

```
php composer.phar require symfony/process:^3.0

```

For the background commands worker, add a controller and command bus middleware in your config

```
'controllerMap' => [
    'background-bus' => [
        'class' => 'mpirogov\bus\console\BackgroundBusController',
    ]
],

'components' => [
        'commandBus' =>[
            ...
            'middlewares' => [
                [
                    'class' => '\mpirogov\bus\middlewares\BackgroundCommandMiddleware',
                    'backgroundHandlerPath' => '@console/yii',
                    'backgroundHandlerRoute' => 'background-bus/handle',
                ]
            ]
            ...
        ]
],
```

Create background command

```
class ReportCommand extends Object implements BackgroundCommand, SelfHandlingCommand
{
    use BackgroundCommandTrait;

    public $someImportantData;

    public function handle($command) {
        // do what you need
    }
}
```

And run it asynchronously!

```
Yii::$app->commandBus->handle(new ReportCommand([
    'async' => true,
    'someImportantData' => [ // data // ]
]))
```

### 3. Queued commands support (optional)

[](#3-queued-commands-support-optional)

#### 3.1 Install required package:

[](#31-install-required-package)

```
php composer.phar require yiisoft/yii2-queue

```

#### 3.2 Configure extensions

[](#32-configure-extensions)

If you need commands to be run in queue, you need to setup middleware and yii2-queue extensions.

```
'components' => [
    'queue' => [
        // queue config
    ],

    'commandBus' =>[
        ...
        'middlewares' => [
            [
                'class' => '\mpirogov\bus\middlewares\QueuedCommandMiddleware',
                // 'delay' => 3, // You can set default delay for all commands here
            ]
        ]
        ...
    ]
]
```

More information about yii2-queue config can be found [here](https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/usage.md)

#### 3.4 Run queue worker

[](#34-run-queue-worker)

`yii queue/listen`More information [here](https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#worker-starting-control)

#### 3.5 Create and run command

[](#35-create-and-run-command)

```
class HeavyComputationsCommand extends Object implements QueuedCommand, SelfHandlingCommand
{
    use QueuedCommandTrait;

    public function handle() {
        // do work here
    }
}

$command = new HeavyComputationsCommand();
Yii::$app->commandBus->handle($command)
```

### 4. Handlers

[](#4-handlers)

Handlers are objects that will handle command execution There are two possible ways to execute command:

#### 4.1 External handler

[](#41-external-handler)

```
return [
    // ...
    'components' => [
        'commandBus' => [
            'class' => 'mpirogov\bus\CommandBus',
            'locator' => [
                'class' => 'mpirogov\bus\locators\ClassNameLocator',
                'handlers' => [
                    'app\commands\SomeCommand' => 'app\handlers\SomeHandler'
                ]
            ]
        ]
    ],
];

// or
$handler = new SomeHandler;
Yii::$app->commandBus->locator->addHandler($handler, 'app\commands\SomeCommand');
// or
Yii::$app->commandBus->locator->addHandler('app\handlers\SomeHandler', 'app\commands\SomeCommand');
```

#### 4.1 Self-handling command

[](#41-self-handling-command)

```
class SomeCommand implements SelfHandlingCommand
{
    public function handle($command) {
        // do what you need
    }
}

$command = Yii::$app->commandBus->handle($command);
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3082d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/19306638?v=4)[Moses Pirogov](/maintainers/mpirogov)[@mpirogov](https://github.com/mpirogov)

---

Top Contributors

[![mpirogov](https://avatars.githubusercontent.com/u/19306638?v=4)](https://github.com/mpirogov "mpirogov (14 commits)")

---

Tags

queueyii2extensioncommand bus

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mpirogov-yii2-command-bus2/health.svg)

```
[![Health](https://phpackages.com/badges/mpirogov-yii2-command-bus2/health.svg)](https://phpackages.com/packages/mpirogov-yii2-command-bus2)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M2.9k](/packages/craftcms-cms)[trntv/yii2-command-bus

Yii2 Command Bus extension

57647.2k8](/packages/trntv-yii2-command-bus)[ignatenkovnikita/yii2-queuemanager

Yii2 Queue Manager

2062.9k2](/packages/ignatenkovnikita-yii2-queuemanager)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
