What is middleware in laravel?

后端 未结 4 1753
野性不改
野性不改 2021-01-19 22:09

I\'m trying to understand how the middleware works in Laravel. Here\'s my class can any one explain how does its works.?



        
4条回答
  •  悲&欢浪女
    2021-01-19 22:33

    Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.

    Reference

    Edit: As explained by @num8er

    Middleware is the function (or logic) that stands between router and route handler.

    In your code:

    public function handle($request, Closure $next)
    {
        if ($request->age <= 200) {
            return redirect('home');
        }
    
        return $next($request);
    }
    

    $request->age is a variable that provided in request and can be checked on each HTTP request, if its value <= 200 then user redirects to home route.

提交回复
热议问题