What is Middleware in Laravel?

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