PHPackages                             trntv/yii2-command-bus - 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. trntv/yii2-command-bus

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

trntv/yii2-command-bus
======================

Yii2 Command Bus extension

3.2.0(8y ago)57625.1k↓25.1%18[4 issues](https://github.com/trntv/yii2-command-bus/issues)8BSD-3-ClausePHP

Since Jan 25Pushed 8y ago10 watchersCompare

[ Source](https://github.com/trntv/yii2-command-bus)[ Packagist](https://packagist.org/packages/trntv/yii2-command-bus)[ RSS](/packages/trntv-yii2-command-bus/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (15)Used By (8)

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

[](#yii2-command-bus)

Command Bus for Yii2

[![Build Status](https://camo.githubusercontent.com/f30a76b04f98ffe841733557a042b3baa090743de642b7aeb168eaafdbf5cc35/68747470733a2f2f7472617669732d63692e6f72672f74726e74762f796969322d636f6d6d616e642d6275732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/trntv/yii2-command-bus)

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 trntv/yii2-command-bus

```

or add

```
"trntv/yii2-command-bus": "^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' => 'trntv\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' => 'trntv\bus\console\BackgroundBusController',
    ]
],

'components' => [
        'commandBus' =>[
            ...
            'middlewares' => [
                [
                    'class' => '\trntv\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' => '\trntv\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' => 'trntv\bus\CommandBus',
            'locator' => [
                'class' => 'trntv\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

45

—

FairBetter than 93% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 60% 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 ~63 days

Recently: every ~178 days

Total

14

Last Release

2948d ago

Major Versions

1.2.3 → 2.0.02016-04-06

2.1.3 → 3.0.02017-08-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/9a67acd28d366ff328006f3e62c314993301a3742af6dbf0da18693cda04c9c6?d=identicon)[trntv](/maintainers/trntv)

---

Top Contributors

[![mpirogov](https://avatars.githubusercontent.com/u/19306638?v=4)](https://github.com/mpirogov "mpirogov (3 commits)")[![Bhoft](https://avatars.githubusercontent.com/u/1777147?v=4)](https://github.com/Bhoft "Bhoft (1 commits)")[![davidjeddy](https://avatars.githubusercontent.com/u/6232455?v=4)](https://github.com/davidjeddy "davidjeddy (1 commits)")

---

Tags

command-busyii2queueyii2extensioncommand bus

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/trntv-yii2-command-bus/health.svg)

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

###  Alternatives

[ignatenkovnikita/yii2-queuemanager

Yii2 Queue Manager

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

PHPackages © 2026

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