1. Go to this page and download the library: Download rabbitevents/listener 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/ */
use RabbitEvents\Listener\Attributes\Listener;
#[Listener(event: 'payment.succeeded')]
class SendNotification
{
public function handle($payload)
{
// ...
}
}
class UserEventSubscriber
{
#[Listener(event: 'user.created')]
public function onUserCreated($payload) {}
#[Listener(event: 'user.deleted')]
public function onUserDeleted($payload) {}
}
/**
* Determine if events and listeners should be automatically discovered.
*/
public function shouldDiscoverEvents(): bool
{
return false;
}
/**
* Get the listener directory path.
*/
protected function listenerDirectory(): string
{
return app_path('Bus/Listeners');
}
class SendNotification
{
public function handle($eventPayload)
{
Mailer::to(Arr::get($eventPayload, 'user.email'))
->subject('Payment Succeeded')
->message('...');
}
}
class AllItemEventsListener
{
public function handle($event, $payload)
{
match($event) => [
'item.created' => ...,
'item.deleted' => ...,
'item.updated' => ...,
];
}
}
use App\Messages\AccountCreated;
class AccountListener
{
public function handle(AccountCreated $message)
{
// $message is an instance of App\Messages\AccountCreated
$id = $message->getId();
}
}
/**
* Handle payment but only if the type is 'mytype'
*
* @param array $payload
* @return void
*/
public function handle($payload)
{
if (\Arr::get($payload, 'entity.type') !== 'mytype') {
return;
}
Entity::find(\Arr::get($payload, 'entity.id'))->activate();
}
namespace App\Listeners\RabbitEvents\Middleware;
class FilterEntities
{
/**
* @param string $event the event name. Passing only for wildcard events
* @param array $payload
*/
public function handle([$event,] $payload)
{
return !\Arr::get($payload, 'entity.type') == 'mytype';
}
}
use App\Listeners\RabbitEvents\Middleware\FilterEntities;
class PaymentListener
{
public array $middleware = [
FilterEntities::class,
`App\Listeners\RabbitEvents\Middleware\AnotherMiddleware@someAction`,
];
/**
* @param string $event the Event Name. Passing only for wildcard events
* @param array $payload
*/
public function middleware([$event, ]$payload)
{
return !\Arr::get($payload, 'entity.type') == 'mytype';
}
}
bash
php artisan rabbitevents:install
bash
php artisan rabbitevents:listen
bash
php artisan rabbitevents:list
bash
php artisan rabbitevents:cache
bash
php artisan rabbitevents:clear
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.