Events Flashcards
In what directory are event classes stored?
Event classes are typically stored in the app/Events directory
In what directory is listener classes stored?
Listeners are stored in app/Listeners
Where is a convenient place to register all of your application’s event listeners?
The EventServiceProvider
What is the purpose of the EventServiceProvider listen property?
The listen property contains an array of all events (keys) and their listeners (values). Of course, you may add as many events to this array as your application requires. For example, let’s add a OrderShipped event:
/** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'App\Events\OrderShipped' => [ 'App\Listeners\SendShipmentNotification', ], ];
What is the purpose of the php artisan event:generate command?
Of course, manually creating the files for each event and listener is cumbersome. Instead, simply add listeners and events to your EventServiceProvider and use the event:generate command. This command will generate any events or listeners that are listed in your EventServiceProvider. Of course, events and listeners that already exist will be left untouched:
php artisan event:generate
What is the purpose of a wildcard event listener?
You may even register listeners using the * as a wildcard parameter, allowing you to catch multiple events on the same listener. Wildcard listeners receive the event name as their first argument, and the entire event data array as their second argument:
Event::listen('event.*', function ($eventName, array $data) { // });
What is the purpose of the SerializeModels trait used by an event class?
The SerializesModels trait used by the event will gracefully serialize any Eloquent models if the event object is serialized using PHP’s serialize function.
What is a event class?
An event class is simply a data container which holds the information related to the event.
What is a listener class?
Event listeners receive the event instance in their handle method. Within the handle method, you may perform any actions necessary to respond to the event:
public function handle(OrderShipped $event) { // Access the order using $event->order... }
How would you stop the propagation of an event?
Sometimes, you may wish to stop the propagation of an event to other listeners. You may do so by returning false from your listener’s handle method.
What is the benefit of utilizing queued event listeners?
Queueing listeners can be beneficial if your listener is going to perform a slow task such as sending an e-mail or making an HTTP request.
How would you specify that a listener should be queued?
To specify that a listener should be queued, add the ShouldQueue interface to the listener class. Listeners generated by the event:generate Artisan command already have this interface imported into the current namespace, so you can use it immediately:
What happens to a queued listener if no exceptions are thrown after being executed by the queue?
The queued job will automatically be deleted after it has finished processing.
How would you customize the queue connection and queue name used by an event listener?
If you would like to customize the queue connection and queue name used by an event listener, you may define $connection and $queue properties on your listener class:
/** * The name of the connection the job should be sent to. * * @var string|null */ public $connection = 'sqs';
/** * The name of the queue the job should be sent to. * * @var string|null */ public $queue = 'listeners';
What is the trait required by a listener to access a queue’s job delete and release methods?
The Illuminate\Queue\InteractsWithQueue trait:
release(30);
}
}
}