How to Create Middleware In Laravel ? With Easy Example

How to Create Middleware In Laravel ?

In previouse Laravel tutorial we have cove the Laravel form validation with example but today you will learn how to create middleware in Laravel.

What is Middleware In Laravel?

In Laravel framework the middleware is use to filter all the HTTP request. For example some condition apply in middleware like age restriction if user age grater then 18 then middleware pass the filter and if user age less then 18 then user redirect to another page.

The middleware check the authentication or rights of user for example you if user try to access the index page without login then middleware check the user login or not if he login then can access otherwise redirect login page.

Create Middleware In Laravel

php artisan make:middleware userage

Types of Middlwware

  1. Global Middleware
  2. Group Middleware
  3. Route Middleware

Global Middleware

In Laravel the Global Middleware is that which use globally and apply all the HTTP request. The authentication or condition declare in Global Middleware it automatically apply in all.

Open the userage middleware which you have created and write the following code. Which is the age condition

 //echo "globel msg for all pages";
        if ($request->age && $request->age<18)
        {

            return redirect('noaccess');
        }
        return $next($request);

How To Apply Middleware Laravel Project?

Open app->HTTP->Karnel.php file copy the following code and past in the first array middleware.

  \App\Http\Middleware\checkage::class,
How to Create Middleware In Laravel
How to Create Middleware In Laravel

Group Middleware

In Group Middleware the condition is apply in the group view which you have included. For example you want to apply age condition in some pages then you can use group middleware.

Apply Group Middleware

Open the karnel.php file in write the array with the name of proatectage.

 'protectpage' =>[
              \App\Http\Middleware\checkage::class,

        ]

The group middleware declare in the web.php file with Route. Now I want to use the above middleware in the some page so I make a route and include the required pages.

Route ::group(['middleware'=>['protectpage']],function(){
Route ::view("contact","/contact");
Route::view("user",'user');
Route ::view("about","/about");  
});

Route Middleware

Route Middleware is use to apply in the single view. It is define in web.php in Route. For example you want to apply the middleware in only one page then you will use this middleware.

Route ::group(['middleware'=>['protectpage']],function(){

How to create middleware in laravel, laravel global middlewre example, laravel group middlewaare example, laravel route middleware example, laravel middleware types, types of middleware in laravel, laravel middleware auth, laravel what is middleware, laravel where middlewaare use, laravel middleware example, laravel middleware exeption.

This Post Has One Comment

Leave a Reply