🐘 PHP
Advanced
What is middleware in PHP frameworks?
Answer
Middleware is a pipeline of components that process HTTP requests and responses. Each middleware wraps the next one in the pipeline — it can inspect/modify the request before passing it forward, or inspect/modify the response on the way back. The PSR-15 standard defines: MiddlewareInterface (with process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface) and RequestHandlerInterface. Common middleware uses: authentication checks, rate limiting, CORS headers, request logging, body parsing, CSRF verification, cache headers. In Laravel, global middleware in app/Http/Middleware wraps every request. Route middleware can be applied selectively: Route::middleware(["auth", "throttle:60,1"])->group().