[Lumen5.2 documentation] more features-events
1. Introduction
Lumen events provide simple observer mode implementation, allowing you to subscribe to and listen to events in the application. The event class is usually stored in the app/Events directory, and the listener is stored in app/Listeners.
2. differences with Laravel
Generally, the event functions in Lumen are almost the same as those in Laravel. for details about how to use the functions, see the Laravel event documentation. Lumen also supports event broadcast, allowing you to listen to server-side events in client JavaScript. It should be noted that there is a difference between Lumen and Laravel events.
Generator
In Lumen, the Artisan command for generating events and listener classes is not provided, so you need to copy ExampleEvent or ExampleListener to customize the event class and listener class.
Register event/listener
Like the Laravel framework, the Lumen built-in EventServiceProvider is used to register all event listeners. The listen attribute is an array containing all events (keys) and their corresponding listener (values). you can add all events of the application to this array:
/*** The event listener mappings for the application. ** @ var array */protected $ listen = ['app \ Events \ exampleevent' => ['app \ Listeners \ ExampleListener ',],];
Trigger Event
You can use the helper function event or Event facade in the application to trigger events. these methods are called like Laravel:
Event (new ExampleEvent); Event: fire (new ExampleEvent );