Ajay Korat
Ajay Korat

Reputation: 701

What is middleware in laravel?

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

<?php

namespace App\Http\Middleware;

use Closure;

class CheckAge
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->age <= 200) {
            return redirect('home');
        }

        return $next($request);
    }

}

Thanks

Upvotes: 4

Views: 6241

Answers (5)

Yousha Aleayoub
Yousha Aleayoub

Reputation: 5715

User Request --> Middleware --> Controller --> Application Logic --> Middleware --> Response

In Laravel, middleware acts as a filter/inspecter for HTTP requests entering your application.

  • Middleware acts as checkpoints that can alter or block requests before they reach the main application.
  • They are reusable across routes, making them efficient for applying common access controls, logging, or security checks.
  • Middleware simplifies keeping your controllers focused on core business logic by handling these common tasks before they even reach the controller.

enter image description here

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26298

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.

Upvotes: 7

Murugan
Murugan

Reputation: 1

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.

Upvotes: 0

BetaDev
BetaDev

Reputation: 4684

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)

Upvotes: 2

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

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

Upvotes: 1

Related Questions