n How to Create Custom Middleware in Laravel 7 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

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

you can see default auth middleware in laravel 7. auth middleware will protect your route url, allow only logged in user in laravel 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 "checkRole" middleware to check if connected user has access to some pages.

So just follow few step to create custom middleware.

 

Create Custom Middleware

In this step, we will create custom middleware using laravel 7 command. So let's open your terminal and run bellow command:

php artisan make:middleware CheckRole

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

app/Http/Middleware/CheckRole.php

<?php

namespace App\Http\Middleware;

use Closure;

class CheckRole
{

    /**
     * Handle an incoming request.
     * @param $request
     * @param Closure $next
     * @param mixed ...$roles
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function handle($request, Closure $next, ...$roles)
    {

        if (in_array($request->header('role'), $roles)) {
            return response()->json(['result' => 'access denied']);
        }
        return $next($request);
    }
}

 

now we need to register this middleware on kernel file. So let's add bellow changes in your controller file:

app/Http/Kernel.php

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    .....
    'role' => \App\Http\Middleware\CheckRole::class,
];

Add Middleware to Routes

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

routes/web.php

Route::group(['middleware' => ['role:admin,user']], function () {

    Route::get('/', function () {
        return view('welcome');
    });

    Route::resource('posts', 'PostController');
    
});

 

I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook