How to create and use Middleware in Laravel 5

Laravel is the best framework in php.  Laravel framework provide saveral functionality and you can also find from this site. But now in this post you can learn middleware. Middleware as a filter in laravel all HTTP requests entering your application.Middleware works between request and response of our application.It sits between Request and Response like a firewall.

What are middlewares in Laravel

In Laravel  middleware as filter that are used to filter all HTTP requests entering your application. Middleware works between request and response of our application. It sits between Request and Response like a

firewall that examine whether to route a request. Laravel has several inbuilt middleware like EncryptCookies, RedirectIfAuthenticated, TrimStrings, TrustProxies etc . All middleware located in app/Http/Middleware folder.

Types of Middleware in laravel

There are three types of middleware in laravel

  1. Global Middleware
  2. Route Middleware Groups
  3. Route Middleware

Create Admin Middleware

In this example, I added middleware for check if user is admin then it can open admin routes. So i added userType column in my users table. If user have userType admin then it can access “admin ” routes.

So first create IsAdmin middleware  using below command.

Now you can found IsAdmin.php  file in app/Http/Middleware directory and open IsAdmin.php file and paste below code in that file.

Now we need to register and create alias of above created middleware in Kernel.php file, So first open Kernel.php file and below line.


Now we are ready to use admin Middleware in  routes.php file, So you can see how to use middleware in routes.php file


I hope you might have understood it properly, keep reading our other blogs posts for more coding tricks.


Leave a Comment