What is 'Middleware' in a PHP web application, and where does it sit in the Request/Response cycle?

Last updated 15 June 2026

  • Middleware is code between the incoming HTTP request and the route handler that filters or modifies requests and responses, layered like an onion.

  • Each layer either passes control on with $next($request) or short-circuits with its own response. It can also modify the response on the way out.

  • Common uses: authentication, CSRF protection, rate limiting, CORS headers, and logging (cross-cutting concerns kept out of controllers).

  • https://www.slimframework.com/docs/v4/concepts/middleware.html
  • https://medium.com/legacybeta/the-role-of-middleware-in-php-frameworks-c80b988d4273
  • https://doeken.org/blog/middleware-pattern-in-php
  • https://dev.to/rohitdhiman/laravel-request-lifecycle-explained-beginner-friendly-guide-3b4
  • https://laravel.com/docs/5.6/lifecycle
  • https://lumen.laravel.com/docs/11.x/middleware
  • https://www.php-fig.org/psr/psr-15/meta/
  • https://www.php-fig.org/psr/psr-15/
All PHP interview questions