What is Middleware in Laravel?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Laravel development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

Middleware is code that runs between an incoming HTTP request and the controller. It is a filtering mechanism — inspecting and optionally transforming requests or responses. Built-in Laravel middleware includes: Authenticate (redirects unauthenticated users), VerifyCsrfToken (validates CSRF tokens on POST requests), ThrottleRequests (rate limiting), EncryptCookies. Create custom middleware: php artisan make:middleware EnsureEmailVerified. Apply to routes: Route::middleware(["auth"])->group(...). Global middleware runs on every request. Middleware can run before the request (guard checks, request modification) or after the response (adding headers, logging).

Common Mistake

Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real Laravel project.