🐘 PHP Advanced

What is middleware in PHP frameworks?

Why Interviewers Ask This

Advanced questions like this reveal whether a candidate has internalized PHP deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.

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().

Pro Tip

Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex PHP answers easy to follow.