PHP code example of deankh / laravel-rabbitmq

1. Go to this page and download the library: Download deankh/laravel-rabbitmq 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/ */

    

deankh / laravel-rabbitmq example snippets


        
        namespace App\Console\Commands;
        use Illuminate\Console\Command;
        use DeanKH\RabbitMQ\Messaging\Pub\PublishInterface;
        use DeanKH\RabbitMQ\Messaging\Pub\Data;
        class PublishCommand extends Command
        {
            protected $signature = 'publish';
            /**
             * @var PublishInterface
             */
            private $publish;

            public function __construct(PublishInterface $publish)
            {
                parent::__construct();
                $this->publish = $publish;
            }

            public function handle()
            {
                $this->publish->route(['test.1', 'test.2', 'test.3'], str_random());
                # Or if you want to send array you can use the dedicated class
                # $this->publish->route(['test.1', 'test.2', 'test.3'], new Data(['hello' => 'world']);
            }
        }
        

        
        namespace App\Console\Commands;
        use Illuminate\Console\Command;
        use PhpAmqpLib\Message\AMQPMessage;
        use DeanKH\RabbitMQ\Messaging\Sub\ConsumeInterface;
        class DummyCommand extends Command
        {
            protected $signature = 'consume';

            /**
             * @var ConsumeInterface
             */
            private $consume;

            public function __construct(ConsumeInterface $consume)
            {
                parent::__construct();
                $this->consume = $consume;
            }

            public function handle()
            {
                $this->consume->route(['test.*'], function ($msg) {
                    return $this->msg($msg);
                });
            }

            private function msg(AMQPMessage $msg)
            {
                $this->line($msg->body);
            }
        }