Laravel 7/6 Custom Middleware Example

Laravel 7/6 Custom Middleware Example

In this post we will give you information about Laravel 7/6 Custom Middleware Example. Hear we will give you detail about Laravel 7/6 Custom Middleware ExampleAnd how to use it also give you demo for it if it is necessary.

This article is focused on how to create custom middleware in laravel 7/6. if you want to see example of laravel 7/6 create your own middleware then you are a right place you will learn how to use custom middleware in laravel 7/6. i explained simply about laravel 7/6 make custom middleware Let’s see bellow example how to make custom middleware in laravel 7/6.

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 laravel 6 application they are provide several default web middleware but in this tutorial we are going to create custom middleware for laravel 6 application.

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 like as bellow example link:

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

As above link, if you are going to pass then you are valid for this middleware, but if you pass another value or you forgot to pass then it gives you json error from custom middleware. So let’s follow bellow step to create custom validation in laravel 6 application.

Step 1: Create Custom Validation

In first step, we have to create custom validation using laravel 6 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 AppHttpMiddleware;


use Closure;


class CheckType

{

/**

* Handle an incoming request.

*

* @param IlluminateHttpRequest $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 simply add this way:

app/Http/Kernel.php

<?php


namespace AppHttp;


use IlluminateFoundationHttpKernel 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' => AppHttpMiddlewareCheckType::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", "HomeController@checkMD")->middleware("checkType");

Also see:Laravel 6 Image Upload Tutorial

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 AppHttpControllers;


use IlluminateHttpRequest;


class HomeController extends Controller

{

public function checkMD()

{

dd('You are in Controller Method');

}

}

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

Also see:How to use Laravel 7/6 Authorization using Gates?

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

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

I hope it can help you…

Hope this code and post will helped you for implement Laravel 7/6 Custom Middleware Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

8  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US