Events
[[toc]]
Introduction
Goravel's events provide a simple observer pattern implementation, allowing you to subscribe and listen for various events that occur within your application. Event classes are typically stored in the app/events
directory, while their listeners are stored in app/listeners
. Don't worry if you don't see these directories in your application as they will be created for you as you generate events and listeners using Artisan console commands.
Events serve as a great way to decouple various aspects of your application, since a single event can have multiple listeners that do not depend on each other. For example, you may wish to send a Slack notification to your user each time an order has shipped. Instead of coupling your order processing code to your Slack notification code, you can raise an app\events\OrderShipped
event which a listener can receive and use to dispatch a Slack notification.
Registering Events & Listeners
The app\providers\EventServiceProvider
included with your Goravel application provides a convenient place to register all of your application's event listeners. The listen
method contains an array of all events (keys) and their listeners (values). You may add as many events to this array as your application requires. For example, let's add an OrderShipped
event:
Generating Events & Listeners
You can use the make:event
and make:listener
Artisan commands to generate individual events and listeners:
Defining Events
An event class is essentially a data container which holds the information related to the event, the Handle method of event
passes in and returns the []event.Arg
structure, you can process data here, the processed data will be passed into all associated listeners
For example, let's assume an app\events\OrderShipped
event:
Defining Listeners
Next, let's take a look at the listener for our example event. Event listeners receive []event.Arg
of the event Handle
method returns. Within the Handle
method, you may perform any actions necessary to respond to the event:
Stopping 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 error from your listener's Handle
method.
Queued Event Listeners
Queued Event Listeners & Database Transactions
When queued listeners are dispatched within database transactions, they may be processed by the queue before the database transaction has committed. When this happens, any updates you have made to models or database records during the database transaction may not yet be reflected in the database. In addition, any models or database records created within the transaction may not exist in the database. If your listener depends on these models, unexpected errors can occur when the job that dispatches the queued listener is processed. At this time, the event needs to be placed outside the database transactions.
Dispatching Events
We can dispatching Events by facades.Event().Job().Dispatch()
method.
event.Arg.Type
Supported Types
event.Arg.Type
Supported TypesLast updated