1. Go to this page and download the library: Download nuwber/rabbitevents 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/ */
// In 9.x: returned void
// In 10.x: returns ?string ($messageId)
$messageId = publish('user.registered', $payload);
use function RabbitEvents\Publisher\publish;
$messageId = publish('user.registered', [
'user_id' => 123,
'email' => '[email protected]',
'created_at' => now()->toIso8601String(),
]);
logger()->info("Published event user.registered with ID: {$messageId}");
namespace App\Events;
use App\Models\User;
use RabbitEvents\Publisher\ShouldPublish;
use RabbitEvents\Publisher\Support\Publishable;
class UserRegistered implements ShouldPublish
{
use Publishable;
public function __construct(public User $user)
{
}
public function publishEventKey(): string
{
return 'user.registered';
}
public function toPublish(): array
{
return [
'user_id' => $this->user->id,
'email' => $this->user->email,
];
}
}
use RabbitEvents\Publisher\ShouldPublish;
use RabbitEvents\Publisher\ShouldPublishAfterCommit;
use RabbitEvents\Publisher\Support\Publishable;
// Option 1: Marker interface
class InvoicePaid implements ShouldPublishAfterCommit
{
use Publishable;
}
// Option 2: Property or method via Publishable trait
class OrderShipped implements ShouldPublish
{
use Publishable;
public bool $afterCommit = true;
}
use Illuminate\Support\Facades\Context;
// In Service A:
Context::add('trace_id', (string) Str::uuid());
publish('order.created', $orderData);
// In Service B (Listener):
// Context::get('trace_id') is automatically restored!
public function handle(array $payload) {
// $payload['key']
}
public function handle(\Google\Protobuf\Internal\Message $message) {
// $message->getSomething()
}
use RabbitEvents\Foundation\Contracts\Serializer;
use RabbitEvents\Foundation\Contracts\ContentType;
use RabbitEvents\Foundation\Contracts\Payload;
class MySerializer implements Serializer
{
public function serialize($data): Payload { ... }
public function deserialize($message): Payload { ... }
public function contentType(): ContentType { return new MyContentType(); }
public function canSerialize($payload): bool { ... }
}
namespace App\BroadcastEvents;
use RabbitEvents\Publisher\ShouldPublish;
use RabbitEvents\Publisher\Support\Publishable;
use RabbitEvents\Publisher\Support\PublishableEventTesting;
class Event implements ShouldPublish
{
use Publishable;
use PublishableEventTesting;
public function __construct(private array $payload)
{
}
public function publishEventKey(): string
{
return 'something.happened';
}
public function toPublish(): array
{
return $this->payload;
}
}
use \App\RabbitEvents\Event;
use \App\RabbitEvents\AnotherEvent;
Event::fake();
Event::publish(['key1' => 'value1', 'key2' => 'value2']);
// Assert event was published
Event::assertPublished();
// Assert with closure
Event::assertPublished(function ($event, $payload) {
return $payload['key1'] === 'value1';
});
// Assert published exact number of times
Event::assertPublishedTimes(1);
// Assert an event was not published
AnotherEvent::assertNotPublished();
// Assert nothing was published
Event::assertNothingPublished();