What is the role of Middleware in a modern PHP framework, and how does it sit between the request and the response?
Last updated
Middleware wraps request handling like onion layers. Each layer sees the request on the way in and the response on the way out (standardized by PSR-15's
MiddlewareInterface::process()).It handles cross-cutting concerns such as authentication, CORS, logging, sessions and rate limiting.
Each layer either calls
$handler->handle($request)to continue and may modify the response, or short-circuits by returning early (e.g. a 401).