PHP code example of muxtorov98 / yii2-rabbitmq-queue
1. Go to this page and download the library: Download muxtorov98/yii2-rabbitmq-queue 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/ */
muxtorov98 / yii2-rabbitmq-queue example snippets
return [
'components' => [
'rabbitPublisher' => [
'class' => RabbitMQQueue\Components\RabbitPublisher::class,
'host' => env('RABBITMQ_HOST'),
'port' => env('RABBITMQ_PORT'),
'user' => env('RABBITMQ_USER'),
'password' => env('RABBITMQ_PASSWORD'),
'vhost' => '/',
],
],
use RabbitMQQueue\Controllers\WorkerController;
use RabbitMQQueue\Registry\HandlerRegistry;
use RabbitMQQueue\Handlers\QueueHandlerInterface;
return [
'components' => [
'rabbitmq' => [
'class' => RabbitMQQueue\Components\RabbitMQConnection::class,
'host' => env('RABBITMQ_HOST'),
'port' => env('RABBITMQ_PORT'),
'user' => env('RABBITMQ_USER'),
'password' => env('RABBITMQ_PASSWORD'),
'vhost' => '/',
],
],
'controllerMap' => [
'worker' => WorkerController::class,
],
'container' => [
'definitions' => [
HandlerRegistry::class => function(\yii\di\Container $container) {
$handlers = [];
foreach (glob(__DIR__ . '/../../console/handlers/*Handler.php') as $file) {
$class = 'console\\handlers\\' . basename($file, '.php');
if (class_exists($class) && is_subclass_of($class, QueueHandlerInterface::class)) {
$handlers[] = $container->get($class);
}
}
return new HandlerRegistry($handlers);
},
],
],
];
Yii::$app->rabbitPublisher->publish('default_queue', ['message' => 'Hello queue']);
namespace console\handlers;
use RabbitMQQueue\Attributes\QueueChannel;
use RabbitMQQueue\Handlers\QueueHandlerInterface;
#[QueueChannel('default_queue')]
class DefaultHandler implements QueueHandlerInterface
{
public function handle(array $data): string
{
if (empty($data)) {
return "👋 Hello RabbitMQ! (empty message)";
}
return "📨 Received: " . json_encode($data, JSON_UNESCAPED_UNICODE);
}
}
bash
php yii worker/start
👷 Listening on default_queue
📨 Received: {"message":"Hello queue"}