ItSolutionStuff.com

Laravel 5.7 Middleware Tutorial With Example

By Hardik Savani • November 5, 2023
PHP Laravel

Today, i will share with you how to create custom middleware in laravel 5.7 application. i write step by step tutorial of use of middleware in php laravel 5.7 project. you will understand how to protect your site using middleware in laravel 5.7.

you can see default auth middleware in laravel 5.7. auth middleware will protect your route url, allow only logged in user in laravel 5.7.

a middleware are used for filter HTTP requests in your web application. One of the basic requirement of any web application is HTTP requests filter, so we have to make is well for example make auth middleware. auth middleware always check if you are going then and then you can access those page.

In this example, i am going to create "checkType" middleware and i will use simply on route, when they route will run you must have to pass "type" parameter with "2" value then and then you can access those request likes.

So just follow few step to create custom middleware.

Step 1: Create Custom Middleware

In first step, we have to create custom middleware using laravel 5.7 command. So let's open your terminal and run bellow command:

php artisan make:middleware CheckType

After above run command you will find one file on bellow location and you have to write following code:

app/Http/Middleware/CheckType.php

<?php


namespace App\Http\Middleware;


use Closure;


class CheckType

{

/**

* Handle an incoming request.

*

* @param \Illuminate\Http\Request $request

* @param \Closure $next

* @return mixed

*/

public function handle($request, Closure $next)

{

if ($request->type != 2) {

return response()->json('Please enter valid type');

}


return $next($request);

}

}

After successfully write logic of middleware then we have to register this middleware on kernel file. So let's add bellow changes in your controller file:

app/Http/Kernel.php

<?php


namespace App\Http;


use Illuminate\Foundation\Http\Kernel as HttpKernel;


class Kernel extends HttpKernel

{

....


/**

* The application's route middleware.

*

* These middleware may be assigned to groups or used individually.

*

* @var array

*/

protected $routeMiddleware = [

....

'checkType' => \App\Http\Middleware\CheckType::class,

];

}

Step 2: Add Route

Now we will create simple route using CheckType middleware. So let's simply open routes.php file and add those route.

routes/web.php

Route::get("check-md",["uses"=>"HomeController@checkMD","middleware"=>"checkType"]);

Step 3: Add Controller Method

Now at last we have to add new controller method checkMD() in your Home Controller. So let's add checkMD() method on HomeController.php file.

app/Http/Controllers/HomeController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;


class HomeController extends Controller

{

public function checkMD()

{

dd('checkMD');

}

}

Ok, now we are ready to run our example, so you can run bellow links and check how custom helper works.

http://localhost:8000/check-md?type=2

http://localhost:8000/check-md?type=1

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 5.7 Modular Structure Application Example

Read Now →

PHP Laravel 5.7 - Create Admin Panel Example

Read Now →

Laravel Comment System Tutorial Example

Read Now →

Laravel 5.7 Import Export Excel to database Example

Read Now →

Laravel 5.7 Guzzle http client POST request example

Read Now →

How to create custom 404 error page in Laravel 5.7?

Read Now →

Laravel 5.7 - Create REST API with authentication using Passport Tutorial

Read Now →

How to create database seeder in Laravel 5.7?

Read Now →

How to send mail using queue in Laravel 5.7?

Read Now →