DEV Community

Sonu Maurya
Sonu Maurya

Posted on

Importance of middleware in Laravel

What is Laravel?

Laravel is an open-source PHP web framework that is free of cost to use. It was designed and developed to create various web applications based on Symphony. 

What is Middleware?

In the Laravel interview questions, it is regularly requested that what the correlation between the two is? The reply to this query is - The Middleware, as you know, is software, which is a connecting platform between the working device and the purposes that are walking on that interface. The functioning of the Middleware is a platform that features on the equal working standards as that of the “if and else loop.”  

  • Global Middleware – Runs on each HTTP related issue. If you want to run Middleware on all the requests, then choose this one.
  • Route Middleware – Runs on a particular HTTP pertaining issue.

Importance of Middleware in Laravel Filtering HTTP requests – 

  1. Verifying user authentication –Various functions that run on the codes processed through Laravel can't enable each person to get entry to and use the platform it provides. For this very purpose, Laravel requires Middleware to determine and procedure whether or not a unique Laravel platform gets entry to request is actual or not. 
  2. Processing HTTP access - Middleware renders a platform to provide a convenient mechanism for handling HTTP requests present in your application. 
  3. A link between the user request and the access – Laravel requires some platform to act as a connecting platform between its programming software and applications as well the users who want to access those. 

Example

  • To verify the authentication user, Laravel includes Middleware.
  • If the Middleware finds the consumer login as authenticated, then it will, in addition, raise ahead with the request to proceed to use the application.
  • If the Middleware finds the person login as unauthenticated, then it will decline the user’s request and redirect them to the domestic screen.

Just appear at a regular code to be run through middleware to determine whether or not to enable the person to get right of entry to to the platform or not. 

namespace App\Http\Middleware;

use Closure;

class Before Middleware

{

    public function handle($request, Closure $next)

    {

        // You can write here your condition

        return $next($request);

    }

}

We have the collection of best interview questions for almost every subject. To know more Best interview questions related to any other topic,keep on visiting our website, and be updated.

Top comments (0)