PHP code example of femike / yii2-amqp-rrobin

1. Go to this page and download the library: Download femike/yii2-amqp-rrobin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

femike / yii2-amqp-rrobin example snippets


return [
    ...
    'components' => [
        ...
        'amqp' => [
            'class' => 'webtoucher\amqp\components\Amqp',
            'host' => '127.0.0.1',
            'port' => 5672,
            'user' => 'your_login',
            'password' => 'your_password',
            'vhost' => '/',
        ],
        ...
    ],
    ...
    'controllerMap' => [
        ...
        'rabbit' => [
            'class' => 'webtoucher\amqp\controllers\AmqpListenerController',
            'interpreters' => [
                'my-exchange' => 'app\components\RabbitInterpreter', // interpreters for each exchange
            ],
            'exchange' => 'my-exchange', // default exchange
        ],
        ...
    ],
    ...
];



namespace app\components;

use webtoucher\amqp\components\AmqpInterpreter;


class RabbitInterpreter extends AmqpInterpreter
{
    /**
     * Interprets AMQP message with routing key 'hello_world'.
     *
     * @param array $message
     */
    public function readHelloWorld($message)
    {
        // todo: write message handler
        $this->log(print_r($message, true));
    }
}


namespace console\controllers;

use webtoucher\amqp\components\Amqp;
use Yii;
use yii\console\Controller;

class ExampleController extends Controller
{
	public function actionIndex($message = 'Wasap Man')
	{
		Yii::$app->amqp->send('my-exchange', 'HelloWorld', $message, Amqp::TYPE_DIRECT, true);
	}
}

$ php composer.phar 
bash
$ php yii rabbit
bash
1st$ php yii rabbit HelloWorld direct true
bash
2nd$ php yii rabbit HelloWorld direct true
bash
$ php yii example 
bash
$ php yii example "same text"