• +91 9806724185,+91 9685733903
  • +91 9806724185

 

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.

laravel

laravel

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) {          
      $table->increments('id');           
      $table->string('name');            
      $table->string('email');         
      $table->string('password');      
      $table->boolean('isAdmin')->nullable();          
      $table->rememberToken();        
      $table->timestamps();    
   }); }

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
BasicAuth
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

Laravel v/s Ruby on Rails: In-depth Comparison of Frameworks

Laravel is a modern web application framework, that design web application easily and rapidly. It can perform the customization of the web application with equal ease. It is an open-source and highly used framework, written Read more…

Laravel Framework

Laravel Homestead Benefits as Your Development Environment

  Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine. No Read more…

Laravel Framework

Laravel Shift : Upgrade Laravel Versions Safely

  Laravel Shift is a service that scans your Laravel application and automatically creates a pull request for upgrading Laravel. All you have to do is just choose a repository and pay some money. Some Read more…