title | author | format | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Laravel Events & Listeners |
Vladimir Lelicanin - SAE Institute |
|
- Laravel provides a simple and robust way of implementing event-driven architecture.
- Laravel's
Event
class provides methods for event registration, dispatching, and listening. - Laravel's
Listener
interface can be implemented to handle events.
- To create a new event class, use the
php artisan event:generate
command. - This will create a new
app/Events
folder and generate a skeleton event class. - Edit the
$broadcastOn
property to define the channel for broadcasting the event. - Add any additional data that should be passed with the event to the
$data
property.
- To dispatch an event, use the
event()
function, passing in an instance of the event class. - All listeners registered for the event will receive the event and can react accordingly.
- Laravel will automatically broadcast any event that has a
$broadcastOn
property defined.
- To create a new listener, use the
php artisan make:listener
command. - This will create a new
app/Listeners
folder and generate a skeleton listener class. - Implement the
handle()
method to define what should happen when the event is received. - Bind the listener to the event in the
EventServiceProvider
.
- Add the listener to the
$listen
property on theEventServiceProvider
. - The key should be the name of the event and the value should be an array of listeners.
- If the listener is a class name, it should be specified as a string.
- If the listener is a closure, it can be specified inline.
- It's possible to bind a listener with variables by passing them as arguments to the
listen()
method. - The variables will be passed as additional parameters to the listener's
handle()
method. - This can be useful for dynamic listener subscriptions.
- Laravel also supports queued listeners.
- To queue a listener, add the
ShouldQueue
interface to the listener class. - Laravel will handle the rest for you, queuing the listener and processing it when resources are available.
- Laravel offers an easy way to link events to models through event broadcasting.
- By default, Laravel will automatically broadcast events that implement the
Illuminate\Broadcasting\InteractsWithSockets
trait. - This trait provides methods to define the channel and event name, and to broadcast data with the event.
- By default, Laravel will discover events within the
app/Events
folder and listeners within theapp/Listeners
folder. - To change this, modify the
$discoveredEvents
and$discoveredListeners
properties inEventServiceProvider.php
. - Alternatively, you can register events and listeners manually.
- To register events and listeners manually, use the
Event::listen()
method. - The first argument is the name of the event.
- The second argument is the listener or an array of listeners.
- You can pass a function as the second argument to define the listeners inline.
- You have the option to use event discovery and manual registration together.
- In this case, Laravel will discover events automatically as well as those defined via
Event::listen()
. - This allows for a high degree of flexibility.
- To stop an event from propagating to other listeners, call the
stopPropagation()
method on the event object. - This can be useful if a listener has taken care of an event and you want to ensure that other listeners don't take action as well.
- To define the order in which listeners should be invoked, use the
priority()
method. - The lower the number, the higher the priority.
- Laravel will call the listeners in priority order.
- To subscribe a listener to multiple events, use the
subscribe()
method in theEventServiceProvider
. - The subscriber class should implement the
Subscriber
interface. - All subscribed events and listeners should be defined within the
subscribe()
method.
- When testing events, you can use Laravel's
Event::fake()
method to override event firing. - After calling
fake()
, events will be queued but not dispatched. - Use the
Event::assertDispatched()
andEvent::assertNotDispatched()
methods to test expected behavior.
- Event broadcasting is implemented using Laravel's WebSockets package.
- To broadcast an event, implement the
ShouldBroadcast
interface in the event class. - Define the channel and payload that should be broadcast.
class OrderShipped implements ShouldBroadcast
{
use InteractsWithSockets;
public function __construct($order)
{
$this->order = $order;
}
public function broadcastOn()
{
return new PrivateChannel('orders.' . $this->order->user_id);
}
public function broadcastWith()
{
return ['id' => $this->order->id];
}
}