Installation
Run composer require disjfa/enqueue-bundle in your Laravel project (note: this is a Symfony bundle, but can be adapted for Laravel via bridge packages like php-enqueue/laravel-ext).
Configure DSN
Add the Enqueue DSN to your .env:
ENQUEUE_DSN=mysql://user:password@localhost:3306/database
(For Laravel, ensure compatibility with the underlying enqueue library, e.g., php-enqueue/amqp-ext or php-enqueue/doctrine-ext.)
Register the Bundle
In config/app.php, add:
'providers' => [
// ...
Disjfa\EnqueueBundle\DisjfaEnqueueBundle::class,
],
Define Routes
Create routes/disjfa_enqueue.php (Laravel) or append to config/routes/disjfa_enqueue.yaml (Symfony):
Route::prefix('enqueue')->group(function () {
Route::get('/consume', [\Disjfa\EnqueueBundle\Controller\ConsumerController::class, 'consume']);
});
First Use Case
Trigger a test message via a command or HTTP endpoint, then verify consumption via the /consume route.
Message Production
Use the EnqueueClient (injected via dependency injection) to publish messages:
use Enqueue\Client\Producer;
use Enqueue\AmqpExt\AmqpConnectionFactory;
$connectionFactory = new AmqpConnectionFactory(['dsn' => env('ENQUEUE_DSN')]);
$producer = new Producer($connectionFactory);
$producer->send(new Message('Hello, Queue!'));
Consumer Setup
Extend Disjfa\EnqueueBundle\Consumer\ConsumerInterface to define custom logic:
namespace App\Consumers;
use Disjfa\EnqueueBundle\Consumer\ConsumerInterface;
use Enqueue\Client\Message;
class MyConsumer implements ConsumerInterface {
public function consume(Message $message) {
// Process message (e.g., parse JSON payload, trigger Laravel jobs)
logger($message->getBody());
}
}
Route Integration
Map consumers to routes in config/routes/disjfa_enqueue.yaml:
disjfa_enqueue.consumer:
path: /consume/my-consumer
methods: [GET]
defaults: { _controller: 'DisjfaEnqueueBundle:Consumer/myConsumer' }
Laravel-Specific Adaptations
php-enqueue/laravel-ext for Laravel service provider integration.Illuminate\Bus\Queueable:
$producer->send(new Message(json_encode(['job' => NewJob::dispatch()])));
Background Processing
Schedule consumers via Laravel’s schedule:run or Symfony’s console commands:
// In a Laravel command
$this->call('enqueue:consume', ['--consumer' => 'my_consumer']);
DSN Compatibility
enqueue-bundle DSN format. For Laravel, ensure the underlying php-enqueue extension (e.g., amqp, doctrine) is installed:
pecl install enqueue-amqp
php-enqueue/console:
vendor/bin/enqueue-consumer --dsn="mysql://..." --queue=default
Route Conflicts
Route::namespace('Disjfa\EnqueueBundle\Controller')->group(...);
Message Serialization
Message objects use raw strings. For Laravel, serialize payloads to JSON:
$producer->send(new Message(json_encode(['data' => $payload])));
Consumer Lifecycle
SIGTERM) or Laravel’s Artisan::terminate().Log Consumer Output
Redirect logs to Laravel’s storage/logs/laravel.log:
Monolog\Logger::getInstance('enqueue')->pushHandler(new StreamHandler(storage_path('logs/enqueue.log')));
Check Queue Backlog
Use enqueue:browse (Symfony) or php-enqueue/console:
vendor/bin/enqueue-browse --dsn="mysql://..."
Custom Transports
Override the EnqueueClient in Laravel’s AppServiceProvider:
$this->app->bind(Producer::class, function () {
return new Producer(new AmqpConnectionFactory(['dsn' => env('ENQUEUE_DSN')]));
});
Middleware for Messages Attach middleware to consumers (e.g., validation, retries):
$consumer->setMiddleware(new RetryMiddleware(3));
Laravel Queue Integration
Proxy Enqueue messages to Laravel’s Illuminate\Queue:
$producer->send(new Message(json_encode(['queue' => 'default', 'payload' => $job])));
Testing
Use enqueue/fake for unit tests:
$this->app->bind(Producer::class, function () {
return new Producer(new FakeConnection());
});
How can I help you explore Laravel packages today?