PHP code example of devyk / yii2-amqp

1. Go to this page and download the library: Download devyk/yii2-amqp 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/ */

    

devyk / yii2-amqp example snippets


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



namespace app\components;

use devyk\amqp\components\AmqpInterpreter;
use app\services\ExampleServiceInterface;


class RabbitInterpreter extends AmqpInterpreter
{
    protected $service;
    
    /**
     * Example of passing custom service as dependency for the AmqpInterpreter
     */
    public function __construct(ExampleServiceInterface $exampleService)
    {
        $this->service = $exampleService;
        parent::__construct();
    }
    
    /**
     * 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));
    }
}
bash
$ php yii rabbit
bash
$ php yii rabbit <queue_name> --exchange=<exchange_name>
bash
$ php yii rabbit <queue_name>, <queue_name_1> --exchange=<exchange_name>
bash
$ php yii rabbit <queue_name>, <queue_name_1> --noAck=true