How do I create a new event listener?

To create a new listener class for one of our events, we can use Artisan to scaffold the listener from the project root directory:

php artisan make:listener UpdateDetails

After adding all the necessary functionality for our listener, we have to make Aero aware of it by adding it to a listen property, wrapped within an event like so:

protected $listen = [
    Registered::class => [
            UpdateDetails::class,
    ]
];

There is also an alternative, for events that should not be queued:

​​Event::listen(OrderPlaced::class, function ($event) {
    $order = $event->order;
    //...
});

 

Extending ManagedListener

In order to use a ManagedListener, we have to have an event extending ManagedEvent. This can be accomplished like so:

use Aero\Events\ManagedHandler;
protected $listen = [
    FormSubmitted::class => [
        ManagedHandler::class,
    ],
];

Articles in this section

Was this article helpful?
0 out of 0 found this helpful