Laravel 7/6 Middleware Example Tutorial

In this tutorial you will learn about the Laravel 7/6 Middleware Example Tutorial and its application with practical example.

In this Laravel 7/6 Middleware Example Tutorial, I will show you how to create custom middleware and to use in laravel based project. In this tutorial you will learn to create and use custom middlewalre in laravel. In this article I will share example to create one custom middleware to check language in query string.

What is Middleware?

In Laravel, Middleware is a filtering mechanism that facilitates filtering of incoming HTTP request before it is routed to particular controller for further processing. Laravel framework includes several in-built middleware, including authentication and CSRF protection. All of these middleware are located in the app/Http/Middleware directory. In addition to the built-in middleware, custom middleware can also be created.

Laravel 7/6 Middleware Example Tutorial

In this step by step laravel middleware tutorial I will demonstrate you with example of active or inactive users. Now we will call middleware in routes to restrict logged user to access that routes, if he/she is blocked by admin.

Step 1: Create Middleware

In this step we will first create a custom middleware in laravel based project. So let’s open your command prompt and run following command :

Now, go to app/http/kernel.php and register your custom middleware here :

app/Http/Kernel.php

After successfully register your middleware in laravel project, go to app/http/middleware and implement your logic here :

app/Http/Middleware/CheckStatus.php

Step 2: Add Route

Now, we will add some route with middleware as following

Step 3: Add Method in Controller’

Now we will create a method to check active or inactive users.

app/Http/Controllers/HomeController.php

In this tutorial we have learn about the Laravel 7/6 Middleware Example Tutorial and its application with practical example. I hope you will like this tutorial.