What is middleware in laravel?

后端 未结 4 1749
野性不改
野性不改 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.

    0 讨论(0)
  • 2021-01-19 22:35

    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.

    Of course, additional middleware can be written to perform a variety of tasks besides authentication. A CORS middleware might be responsible for adding the proper headers to all responses leaving your application. A logging middleware might log all incoming requests to your application.

    https://laravel.com/docs/5.4/middleware#introduction

    Middleware is a series of wrappers around your application that decorate the requests and the responses in a way that isn't a part of your application logic.

    https://mattstauffer.co/blog/laravel-5.0-middleware-filter-style

    0 讨论(0)
  • 2021-01-19 22:38

    Middleware main objective is to restrict the unwanted action and here you can check the user given input values and you can allow is valid only.

    0 讨论(0)
  • 2021-01-19 22:52

    As you can see what the middleware is, now lets see the code

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

    This code check every request and check the age variable in the request. If the age is less than 200 then the request will be redirect to the home otherwise it will go to the requesting page. Suppose you are requesting /about page but if you can not pass the middleware condition you will be redirected to /home otherwise to /about i.e. given by return $next($request);. Similary works with auth and cors middleware. You can similarly do some check like $request->user->role=='admin' and redirect to admin page or to other page.

    return $next($request); this gives you the next requesting route (the original route that have requested)

    0 讨论(0)
提交回复
热议问题