When we Work on Laravel Development, we have to manage a lot of requests. Most of the time each request needs to maintain with specific filter requirements. In this context, laravel provide Middleware with a very convenient way to manage application request.



What Middleware Does?

Middleware provides 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.

Today, I will show you how Middleware works & how to create a Middleware with a basic authentication mechanism and successfully implement it in your laravel application.

Laravel Admin Middleware.

Now, To checking that if the current user is administrator or not. So go to your users’ table migration file and add one more field called isAdmin and its data type is boolean.

public function up()     
    Schema::create('users', function (Blueprint $table) {          
   }); }

Implement Auth in Middleware :

Basically, all the Middleware files are located in app/Http/Middleware directory. So our newly created Middleware file will also have to put inside the app/Http/Middleware directory.

The artisan command to create new Middleware is make:middleware

We will create a Middleware that will responsible for BasicAuth. To create a new middleware name BasicAuth run command php artisan make:middleware
after that it will create a middleware class BasicAuth.php inside directory app/Http/Middleware.

Types of Middlewares in Laravel?

There are three types of middleware are available in Laravel.

  • • Global Middleware
  • • Route Middleware groups
  • • Route Middleware

Global Middleware: They run on every HTTP request of application can be assigned in “$middleware” array of Kernel class which is located at app/Http/Kernel.php.

Route Middleware groups: They run on a specific route group of applications and can be assigned by listing your middleware class in “$middlewareGroups” array of Kernel class which is located at app/Http/Kernel.php.

Route Middleware: They run on a specific route of application. To assign route middleware on specific routes, you should first assign your middleware a key with class in “$routeMiddleware” array of your app/Http/Kernel.php file.


Final Words :

This is a very simple implementation of middleware with basic authentication. Middleware has truly some advanced functionalities.

Related Posts

Laravel Framework

October CMS : Best Choice for Laravel CMS

OctoberCMS is the free, efficient, and open-source content management system based on a PHP programming language called "Laravel". OctoberCMS makes the creation and optimisation of websites easy, with fast-paced applications and unique solutions for developers. Read more…

Laravel Framework

Integrate Laravel CMS for Development Process

To build web applications with desired functionalities, developers use different PHP frameworks. Each framework provides specific features that allows developers to build project functionalities easily. Laravel is one of the top PHP frameworks available to Read more…

Laravel Framework

Laravel 7 Released: New Features

Laravel 7 is now released and includes many new features including Laravel Airlock, better routing speed, custom Eloquent casts, Blade component tags, fluent string operations, a new HTTP client, CORS support, and many more features. Read more…