PHP code example of muxtorov98 / rabbitmq-universal

1. Go to this page and download the library: Download muxtorov98/rabbitmq-universal 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 / rabbitmq-universal example snippets




namespace App\Handlers;

use RabbitMQQueue\Core\QueueHandlerInterface;
use RabbitMQQueue\Core\QueueChannel;
use RabbitMQQueue\Core\RabbitPublisher;

#[QueueChannel('notification_queue')]
class NotificationHandler implements QueueHandlerInterface
{
    public function handle(array $message): void
    {
        echo "📩 Notification received: " . json_encode($message, JSON_UNESCAPED_UNICODE) . PHP_EOL;
    }
}

'controllerMap' => [
    'worker' => [
        'class' => \RabbitMQQueue\Frameworks\Yii2\WorkerController::class,
    ],
],



namespace console\Handlers;

use RabbitMQQueue\Core\QueueHandlerInterface;
use RabbitMQQueue\Core\QueueChannel;
use RabbitMQQueue\Core\RabbitPublisher;

#[QueueChannel('email_queue')]
class EmailHandler implements QueueHandlerInterface
{

    public function handle(array $message): void
    {
        echo "📩 Email received: " . json_encode($message, JSON_UNESCAPED_UNICODE) . PHP_EOL;
    }
}



namespace App\Handlers;

use RabbitMQQueue\Core\QueueHandlerInterface;
use RabbitMQQueue\Core\QueueChannel;
use RabbitMQQueue\Core\RabbitPublisher;

#[QueueChannel('log_queue')]
class LogHandler implements QueueHandlerInterface
{

    public function handle(array $message): void
    {
        echo "📩 log received: " . json_encode($message, JSON_UNESCAPED_UNICODE) . PHP_EOL;
    }
}

use RabbitMQQueue\Core\RabbitPublisher;

$publisher = new RabbitPublisher();
$publisher->publish('email_queue', [
    'to' => '[email protected]',
    'subject' => 'Universal publish test!'
]);



namespace App\Console\Commands;

use Illuminate\Console\Command;
use RabbitMQQueue\Core\RabbitPublisher;

class RabbitPublishCommand extends Command
{
    protected $signature = 'rabbit:publish {queue} {--data=}';
    protected $description = 'Publish a message to RabbitMQ queue (from Laravel)';

    public function handle()
    {
        $queue = $this->argument('queue');
        $data = $this->option('data')
            ? json_decode($this->option('data'), true)
            : ['text'=>'Salom from Laravel'];

        $publisher = new RabbitPublisher();
        $publisher->publish($queue, $data);

        $this->info("📩 Message published to queue '{$queue}' from Laravel: " . json_encode($data, JSON_UNESCAPED_UNICODE));
    }
}



namespace App\Command;

use RabbitMQQueue\Core\RabbitPublisher;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
    name: 'rabbit:publish',
    description: 'Publish a message to RabbitMQ queue'
)]
class RabbitPublishCommand extends Command
{
    protected function configure(): void
    {
        $this
            ->addArgument('queue', InputArgument::REQUIRED, 'Queue name')
            ->addArgument('data', InputArgument::REQUIRED, 'JSON encoded data');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $queue = $input->getArgument('queue');
        $jsonData = $input->getArgument('data');

        $data = json_decode($jsonData, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            $output->writeln('<error>❌ JSON format xato:</error> ' . json_last_error_msg());
            return Command::FAILURE;
        }

        $publisher = new RabbitPublisher();
        $publisher->publish($queue, $data);

        $output->writeln("✅ Xabar jo'natildi: <info>$queue</info>");
        return Command::SUCCESS;
    }
}


namespace console\controllers;

use yii\console\Controller;
use RabbitMQQueue\Core\RabbitPublisher;
use yii\helpers\Json;

class RabbitPublishController extends Controller
{
    public $data;

    public function options($actionID)
    {
        return ['data'];
    }

    public function actionIndex($queue)
    {
        $data = $this->data ? Json::decode($this->data) : [];

        $publisher = new RabbitPublisher();
        $publisher->publish($queue, $data);

        $this->stdout("Message published to queue '{$queue}' from Yii2\n");
    }
}
bash
php artisan rabbit:worker

docker compose exec php php artisan rabbit:worker
bash
php yii worker/start
bash
php bin/console rabbit:publish notification_queue '{"event":"user_registered","user_id":12345,"text":"Salom from Symfony"}'