PHP code example of sobirjonovs / laravel-rabbit

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

    

sobirjonovs / laravel-rabbit example snippets


composer 



use App\Rabbitmq\Rabbit\Client;
use PhpAmqpLib\Message\AMQPMessage;
    
$client = app(Client::class);
$client->consume('queue-one', function (AMQPMessage $message) {
            /**
             * Acknowledge a message
             */
            $message->ack(true);
            
            /**
             * @var Client $this
             */
            $this->dispatchEvents($message);
        })->wait(); // or waitForever();




use App\Rabbitmq\Rabbit\Client;
use PhpAmqpLib\Message\AMQPMessage;
    
$client = app(Client::class);
$client->setMessage([
            'method' => $method,
            'params' => $object->all()
])->publish('queue-one');




use App\Rabbitmq\Rabbit\Client;
use PhpAmqpLib\Message\AMQPMessage;
    
$client = app(Client::class);
$result = $client->setMessage([
            'method' => 'methodName',
            'params' => ['param1' => 'value1']
])->request()->getResult(); # you can pass a queue name inside a request method, otherwise it uses the default queue



    .
    .
    .
    /**
     * Namespace of data service classes
     *
     * It will be merged to root namespace which is App/config('amqp.service_namespace')
     */
    'service_namespace' => 'Services',

    /**
     * Namespace of data transfer object
     *
     * It will be merged to root namespace which is App/config('amqp.service_namespace')/config('amqp.dto_namespace')
     */
    'dto_namespace' => 'Dto',
];


    .
    .
    . 
    /**
     * When error is out in service class, that message will be sent to config('amqp.dead-letter-queue')
     *
     * This works only subscriber and publisher mode
     */
    'dead_letter_queue' => 'dead-letter-queue',

    /**
     * When message cannot pass validation, the message will be sent to this queue
     * If name of this queue is null, the message will be deleted
     *
     * This works only subscriber and publisher mode
     */
    'invalid_letter_queue' => null,
];

    ...
    'config' => [

        'default_queue' => 'default',

        'is_multi_queue' => false,

        'queues' => ['default', 'foo', 'bar'],
    ],
bash
php artisan rabbit:install
bash
php artisan make:rabbit-service ProductService createProduct