In this episode, we dig into how middleware works by walking through building our very own middleware from scratch. We kick things off by creating a new middleware class and making sure it implements the correct interface (using PSR standards). Then, we go step-by-step through how the middleware actually plugs into our application and the overall request/response cycle.
We also talk a lot about the middleware "stack" – basically, how multiple middleware can be layered together, each one having a shot at the request before it reaches your route. Think of it like layers of an onion: each middleware wraps around the next, doing its job on both the way in and the way out.
To make things practical, we demonstrate how to set up "before" middleware (code that runs before your main controller does) as well as "after" middleware (code that runs after the controller has done its thing, before the response goes back out).
We finish up with some quick experiments to make sure our middleware is firing as expected and talk about common use-cases, like route protection and persisting form data after validation errors. By the end, you'll have a good sense of how to write and stack middleware, setting you up for the more specific examples coming up in the next episodes!