PHPackages                             vlodkow/yii2-queue - 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. vlodkow/yii2-queue

ActiveLibrary[Queues &amp; Workers](/categories/queues)

vlodkow/yii2-queue
==================

Queue component for Yii2

0291

Since Dec 23Compare

[ Source](https://github.com/ukiepro/yii2-queue)[ Packagist](https://packagist.org/packages/vlodkow/yii2-queue)[ RSS](/packages/vlodkow-yii2-queue/feed)WikiDiscussions Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Queue Component for Yii2
========================

[](#queue-component-for-yii2)

This provides queue component for Yii2 (fork urbanindo/yii2-queue).

Update: no server load + better readme to start using this package.

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

[](#installation)

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

Either run, add:

```
"repositories": [{
    "type": "package",
    "package": {
      "name": "vlodkow/yii2-queue",
      "version": "1.3.7",
      "source": {
        "url": "git@github.com:vlodkow/yii2-queue.git",
        "type": "git",
        "reference": "1.3.7"
      }
    }
  }],
  "require": {
    "vlodkow/yii2-queue": "1.3.*"
  }

```

to the require section of your `composer.json` file.

To use Redis queue or RabbitMQ, you have to add `yiisoft/yii2-redis:*` or `videlalvaro/php-amqplib: 2.5.*` respectively.

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

[](#setting-up)

After the installation, first step is to set the console controller (config/console.php).

```
return [
    // ...
    'controllerMap' => [
        'queue' => 'Vlodkow\Yii2\Queue\Console\Controller',
        'sleep' => 2,
        'rocket_chat_url' => ''
    ],
];
```

For the task worker, set a new module, e.g. `task` and declare it in the config (config/console.php).

```
'modules' => [
    'task' => [
        'class' => 'app\modules\task\Module',
    ]
]
```

And then set the queue component. Don't forget to set the module name that runs the task in the component (config/console.php and config/web.php). For example, queue using AWS SQS:

```
'components' => [
    'queue' => [
        'class' => 'Vlodkow\Yii2\Queue\Queues\SqsQueue',
        'module' => 'task',
        'url' => 'https://sqs.ap-southeast-1.amazonaws.com/123456789012/queue',
		'config' => [
			'key' => 'AKIA1234567890123456',
			'secret' => '1234567890123456789012345678901234567890',
			'region' => 'ap-southeast-1',
			'version' => 'latest'
        ],
    ]
]
```

Or using Database queue

```
'components' => [
    'db' => [
        //the db component
    ],
    'queue' => [
        'class' => 'Vlodkow\Yii2\Queue\Queues\DbQueue',
        'db' => 'db',
        'tableName' => 'queue',
        'module' => 'task',
    ]
]
```

Usage
-----

[](#usage)

For mysql import table from db.sql

### Creating A Worker

[](#creating-a-worker)

Creating a worker is just the same with creating console or web controller. In the task module create a controller that extends `Vlodkow\Yii2\Queue\Worker\Controller`

e.g.

```
class FooController extends Vlodkow\Yii2\Queue\Worker\Controller {

    public function actionBar($param1, $param2){
        echo $param1;
    }
}
```

To prevent the job got deleted from the queue, for example when the job is not completed, return `false` in the action. The job will be run again the next chance.

e.g.

```
class FooController extends Vlodkow\Yii2\Queue\Worker\Controller {

    public function actionBar($param1, $param2){
        try {
        } catch (\Exception $ex){
            \Yii::error('Ouch something just happened');
            return false;
        }
    }
}
```

### Running The Listener

[](#running-the-listener)

To run the listener, run the console that set in the above config. If the controller mapped as `queue` then run.

```
yii queue/listen

```

### Posting A Job

[](#posting-a-job)

To post a job from source code, put something like this.

```
use Vlodkow\Yii2\Queue\Job;

$route = 'foo/bar';
$data = ['param1' => 'foo', 'param2' => 'bar'];
Yii::$app->queue->post(new Job(['route' => $route, 'data' => $data]));
```

Job can also be posted from the console. The data in the second parameter is in JSON string.

```
yii queue/post 'foo/bar' '{"param1": "foo", "param2": "bar"}'

```

Job can also be posted as anonymous function. Be careful using this.

```
Yii::$app->queue->post(new Job(function(){
    echo 'Hello World!';
}));
```

### Deferred Event

[](#deferred-event)

In this queue, there is a feature called **Deferred Event**. Basically using this feature, we can defer a process executed after a certain event using queue.

To use this, add behavior in a component and implement the defined event handler.

```
    public function behaviors() {
        return array_merge([
            [
                'class' => \Vlodkow\Yii2\Queue\Behaviors\DeferredEventBehavior::class,
                'events' => [
                    self::EVENT_AFTER_VALIDATE => 'deferAfterValidate',
                ]
            ]
        ]);
    }

    public function deferAfterValidate(){
        //Do something here.
    }
```

**NOTE**Due to reducing the message size, the `$event` object that usually passed when triggered the event will not be passed to the deferred event. Also, the object in which the method invoked is merely a clone object, so it won't have the behavior and the event attached in the original object.

As for `ActiveRecord` class, since the object can not be passed due to limitation of SuperClosure in serializing PDO (I personally think that's bad too), the behavior should use `\Vlodkow\Yii2\Queue\Behaviors\ActiveRecordDeferredEventBehavior`instead. The difference is in the object in which the deferred event handler invoked.

Since we can not pass the original object, the invoking object will be re-fetched from the table using the primary key. And for the `afterDelete` event, since the respective row is not in the table anymore, the invoking object is a new object whose attributes are assigned from the attributes of the original object.

### Web End Point

[](#web-end-point)

We can use web endpoint to use the queue by adding `\Vlodkow\Yii2\Queue\Web\Controller`to the controller map.

For example

```
    'controllerMap' => [
        'queue' => [
            /* @var $queue Vlodkow\Yii2\Queue\Web\Controller */
            'class' => 'Vlodkow\Yii2\Queue\Web\Controller',
        ]
    ],
```

To post this use

```
curl -XPOST http://example.com/queue/post --data route='test/test' --data data='{"data":"data"}'

```

To limit the access to the controller, we can use `\yii\filters\AccessControl` filter.

For example to filter by IP address, we can use something like this.

```
    'controllerMap' => [
        'queue' => [
            /* @var $queue Vlodkow\Yii2\Queue\Web\Controller */
            'class' => 'Vlodkow\Yii2\Queue\Web\Controller',
            'as access' => [
                'class' => '\yii\filters\AccessControl',
                'rules' => [
                    [
                        'allow' => true,
                        'ips' => [
                            '127.0.0.1'
                        ]
                    ]
                ]
            ]
        ]
    ],
```

Testing
-------

[](#testing)

To run the tests, in the root directory execute below.

```
./vendor/bin/phpunit

```

Road Map
--------

[](#road-map)

- Add more queue provider such as MemCache, IronMQ, RabbitMQ.

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5967302?v=4)[Vlodkow](/maintainers/vlodkow)[@vlodkow](https://github.com/vlodkow)

---

Top Contributors

[![VlodkoMr](https://avatars.githubusercontent.com/u/57656508?v=4)](https://github.com/VlodkoMr "VlodkoMr (13 commits)")

### Embed Badge

![Health badge](/badges/vlodkow-yii2-queue/health.svg)

```
[![Health](https://phpackages.com/badges/vlodkow-yii2-queue/health.svg)](https://phpackages.com/packages/vlodkow-yii2-queue)
```

###  Alternatives

[league/geotools

Geo-related tools PHP 7.3+ library

1.4k5.5M30](/packages/league-geotools)[illuminate/bus

The Illuminate Bus package.

6045.5M508](/packages/illuminate-bus)[uecode/qpush-bundle

Asynchronous processing for Symfony using Push Queues

1672.5M2](/packages/uecode-qpush-bundle)[jayazhao/think-queue-rabbitmq

为 ThinkPHP5.1 队列增加 RabbitMQ 驱动

141.5k](/packages/jayazhao-think-queue-rabbitmq)[mayconbordin/l5-stomp-queue

Stomp Queue Driver for Laravel 5

121.1k](/packages/mayconbordin-l5-stomp-queue)

PHPackages © 2026

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