I've been thinking about the most optimal way to implement middleware in my web application and it got me thinking, how is middleware any different from events?
Let's say that before a controller gets executed I want to verify that the IP from which the request is coming from is not blacklisted. Do I create a new middleware or register a listener for a OnRequest
or BeforeController
event? Don't these two achieve the same thing? They both get executed before the controller and have the ability to take actions based on the contents of the request.
I suppose they differ in the way that middleware is more focused around the before/after controller idea where as events can be used for all kinds of things in the entire application.
There's also the fact that you possibly don't want to execute that IP check on every request so you'd have to have a way to exclude/include the check for certain controllers. This would be somewhat hacky to do with events since the whole idea behind them is that their listeners get called every time that event gets fired.
Are these the only differences though or there's something I'm missing?