I was looking for the RequestHandled event in Lumen. Unfortunately, I was not able to find it.
This event can be found in Laravel through the Illuminate/Foundation/Http/Events namespace. Lumen does not have reference of this namespace though, since it has a different dependency. To compensate, we can make use of an after-middleware instead.
Solution
Reference the code for Laravel’s RequestHandled event. We will be using this in our middleware later:
Here is the code for our RequestHandleMiddleware after-middleware.
It fires after all middleware have finished processing. Here, it simply passes the request and response to our own homemade RequestHandled event. The event is then passed to all listeners.
Do not forget to register the middleware in your app/bootstrap.php, like so:
Closing Thoughts
Now, you can go ahead and listen to our homemade RequestHandled event! This taught me to look at how something is implemented under-the-hood. We can definitely implement the missing parts on our own; especially when the source code is available for us to read.
Feel free to discuss any performance complications of this method if you have found any. This article was inspired by a discussion made here.
The discussion for this post can be found
here
as well.