Monday, February 17, 2020

Create Custom Middleware in Laravel 6 With example

Create Custom Middleware in Laravel 6 With example

This article is about creating custom middleware in Laravel 6. If you want to see an example of Laravel 6, create your own middleware. Then, in the right place, learn how to use custom middleware in Laravel 6. I simply explained how Laravel 6 executes middleware custom Let's look at the example below of how custom middleware is created in Laravel 6.

The middleware is used to filter HTTP requests in your web application. One of the basic requirements for any web application is the HTTP request filter. For example, we need to run authentication middleware. The authentication middleware always checks whether you are on the move and can then access these pages. Several standard web middleware are provided in the Laravel 6 application. However, this tutorial creates custom middleware for the Laravel 6 application.

In this example I will create a middleware "checkType" and just use it on the route. When the route is executed, you must pass the parameter "type" with the value "2", and then you can access the requirements via the following link Example:

http://localhost:8000/check-md?type=2
As with the previous link, it is valid for this middleware if it will happen. However, if it passes another value or has forgotten to pass, a custom middleware JSON error is displayed. So let's follow the next step to create a custom validation in the Laravel 6 application.


Step 1: Generate Custom Validation


The first step is to create a custom validation using the laravel 6 command. So open your terminal and run the following command:

php artisan make:middleware CheckType

After the previous execution command, you will find a file in the following location and need to write the 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 the middleware logic has been successfully written, we need to register this middleware in the core file. So let's just add the following:

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 create a simple route with the CheckType middleware. So let's just open the route.php file and add this route.

routes/web.php

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

Step 3: Add Controller Method


Now we finally have to add a new checkMD () control method to your home controller. So let's add the checkMD () method to the 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('You are in Controller Method');

    }

}


Ok, now we can do our example. So you can follow the links below and check how the custom wizard works.

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

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

I hope This Could help you...

0 comments:

Post a Comment

Please don't enter any spam link in the comment box.