- Events provide a simple observer implementation, allowing you to subscribe and listen for various events that occur in your application.
- Events are classes that can be fired from anywhere in your application.
- An event class will usually be bound to one or many Events Listeners Classes or has those Listeners registered to listen to it.
- "fire" is the term that is usually used to call an Event.
More details here.
- Events can be fired from Actions and or Tasks. It's preferable to choose one place only. (Tasks are recommended).
- Events SHOULD be created inside the Containers. However, general Events CAN be created in the Port layer.
- Event classes CAN be placed inside the Containers in Events folders or on the Ship for the general Events.
- All Events MUST extend from
Before you can use events you need to add the
EventServiceProvider to the
MainServiceProvider of the Ship (if this has not been registered so far). See example below.
In Laravel, you can create and register events in multiple way. Below is an example of an Event that handles itself.
Event Class Example:
Note: You will get more benefits creating Events Listeners for each Event.
To do this you will need to create a custom
EventServiceProvider in your container extending
EventServiceProvider needs to be registered in the containers
MainServiceProvider as well.
You can dispatch an Event from anywhere you want (ideally from Actions and Tasks).
Example: Dispatching the Event class from the example above
Events can implement
Illuminate\Contracts\Queue\ShouldQueue to be queued.
You can handle jobs on dispatching an event.
To do so, you need to implement one of the following interfaces:
This will force you to implement the
handle method and will make apiato execute the method upon dispatching the event.
ShouldHandleNowInterface will make the event execute the handle method as soon as the event gets dispatched.
ShouldHandleInterface will create an eventjob and execute the handle method async (through laravel jobs).
Note: to define Broadcasting route go to