PHP code example of wlgns5376 / laravel-amqp

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

    

wlgns5376 / laravel-amqp example snippets



return [
    'host'     => env('AMQP_HOST', 'localhost'),
    'port'     => env('AMQP_PORT', 5672),
    'user'     => env('AMQP_USER', 'guest'),
    'password' => env('AMQP_PASSWORD', 'guest'),
    'vhost'    => env('AMQP_VHOST', '/'),
    'options'  => [
        'queue'         => '',
        'exchange'      => '',
        'exchange_type' => 'direct',
        'consumer_tag'  => '',
        'passive'       => false,
        'durable'       => false,
        'auto_delete'   => true,
        'exclusive'     => false,
        'binding_key'   => [],
    ],
];


return [
    ...
    'options'  => [
        'queue'         => '',
        'exchange'      => 'topic_logs',
        'exchange_type' => 'topic',
        ...
        'binding_key'   => [
            'kern.*',
            '*.critical'
        ],
    ],
];



namespace App\Jobs;

...
use Wlgns5376\LaravelAmqp\Publisher;

class SampleJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Publisher $publisher)
    {
        $publisher->publish('A critical kernel error', 'kern.critical');
    }
}


$publisher->publish('A critical kernel error', 'kern.critical', [
    'exchange' => 'other_topic_logs',
    'durable' => true,
]);



namespace App\Console\Commands;

use Illuminate\Console\Command;
use Wlgns5376\LaravelAmqp\Consumer;

class ConsumeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'amqp:consume';

    /**
     * Execute the console command.
     * 
     * @param Wlgns5376\LaravelAmqp\Consumer $consumer
     *
     * @return int
     */
    public function handle(Consumer $consumer)
    {
        $consumer->consume(function($message) {
            echo ' [x] ', $message->delivery_info['routing_key'], ':', $message->body, "\n";
        });

        return 0;
    }
}


$consumer->consume(function($message) {
    echo ' [x] ', $message->delivery_info['routing_key'], ':', $message->body, "\n";
}, [
    'exchange' => 'other_topic_logs',
    'durable' => true,
]);
sh
php artisan vendor:publish --tag=amqp