PHP code example of wuwx / laravel-plus-amqp

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

    

wuwx / laravel-plus-amqp example snippets


// config/app.php
'providers' => [
    Wuwx\LaravelPlusAmqp\LaravelPlusAmqpServiceProvider::class,
],

'aliases' => [
    'Amqp' => Wuwx\LaravelPlusAmqp\AmqpFacade::class,
],

return [
    'defaults' => [
        'connection' => 'default',
        'exchange' => 'default',
        'queue' => 'default',
    ],
    'connections' => [
        'default' => [
            'host' => env('AMQP_HOST', 'localhost'),
            'port' => env('AMQP_PORT', 5672),
            'vhost' => env('AMQP_VHOST', '/'),
            'login' => env('AMQP_LOGIN', 'guest'),
            'password' => env('AMQP_PASSWORD', 'guest'),
        ],
    ],
    'exchanges' => [
        'default' => [
            'connection' => 'default',
            'name'       => 'exchange_name',
            'type'       => 'topic',
            'flags'      => AMQP_DURABLE,
        ],
    ],
    'queues' => [
        'default' => [
            'connection' => 'default',
            'bindings' => [
                ['exchange_name', 'routing_key'],
            ],
        ],
    ],
];

use Wuwx\LaravelPlusAmqp\AmqpFacade as Amqp;

Amqp::publish(
    'payload',
    'routing.key',
    AMQP_NOPARAM,
    ['content_type' => 'text/plain']
);

use Wuwx\LaravelPlusAmqp\AmqpFacade as Amqp;

Amqp::consume(function ($message, $queue) {
    // 处理消息
    $queue->ack($message->getDeliveryTag());
});

$manager = app('amqp');

$connection = $manager->connection('default');
$exchange = $manager->exchange('default');
$queue = $manager->queue('default');

$exchange->publish('payload', 'routing.key');
bash
php artisan vendor:publish --tag=config

.
├── config
│   └── amqp.php              # 默认配置
├── src
│   ├── AmqpFacade.php         # Facade
│   ├── AmqpManager.php        # 管理器(connection/exchange/queue)
│   └── LaravelPlusAmqpServiceProvider.php
├── tests
│   └── TestCase.php
└── composer.json