Laravel Advance | Laravel Middleware Tutorial With Example

Raviya Technical
2 min readNov 7, 2021

Today, I will share with you how to create custom middleware in the laravel application. I write step by step tutorial on the use of middleware in the PHP laravel project. you will understand how to protect your site using middleware in laravel.

you can see default auth middleware in laravel. auth middleware will protect your route URL, allow only logged-in users in laravel.

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

In this example, I am going to create “checkType” middleware and I will use simply on route when the route will run you must have passed “type” parameter with “2” value then and then you can access that request likes.

So just follow a few steps to create custom middleware.

Step 1: Create Custom Middleware

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

php artisan make:middleware CheckType

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

app/Http/Middleware/CheckType.php

<?phpnamespace 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 writing the logic of middleware then we have to register this middleware on the kernel file. So let’s add the below changes to your controller file

app/Http/Kernel.php

<?phpnamespace 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 a simple route using CheckType middleware. So let’s simply open the routes.php file and add those routes.

routes/web.php

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

Step 3: Add Controller Method

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

app/Http/Controllers/HomeController.php

<?phpnamespace 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 the below links and check how custom helper works.

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

--

--