Middleware is a layer that inspects or modifies an HTTP request before it reaches the application endpoint and can also process the outgoing response.
Conceptual flow:
Request -> Middleware -> Controller -> Response -> Middleware -> Client
Example:
public function handle($request, Closure $next)
{
if (!$request->user()) {
return redirect('/login');
}
return $next($request);
}
Common uses include authentication, authorization, rate limiting, logging, CORS, maintenance mode, and request normalization.
Scenario:
An /admin area can be protected with authentication and authorization middleware so that unauthenticated or unauthorized requests never reach the controller.
0 comments:
Post a Comment
Thanks