How To Create Custom Middleware In Laravel

Websolutionstuff | Aug-24-2020 | Categories : Laravel

In this article, we will give you information about middleware and we will see how to create custom middleware in laravel 7 and laravel 8.

Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. If your project has multiple users then we need to use middleware to provide different access or login to different users.

So, let's see how to create custom middleware in laravel 7/8 and create custom middleware in laravel 8.

In this example, I have created "roleType" middleware and I will use it simply on the route when the route will run you must have to pass the "type" parameter, and then you can access those requests like as below demo link.

http://localhost:8000/check/role?type=user
http://localhost:8000/check/role?type=admin

 

 

Step 1: Create Middleware

First, we need to create custom middleware. So, run the below command in your terminal.

php artisan make:middleware RoleType

After running the above command, you will find one file on the app/Http/Middleware/RoleType.php location and you have to add the below code.

<?php

namespace App\Http\Middleware;

use Closure;

class RoleType
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->type != 'admin') {
            return response()->json('Please enter valid type');
        }

        return $next($request);
    }
}

 

Step 2: Register This Middleware on Kernel File

After successfully adding this code of middleware we have to register this middleware on the kernel file.

app/Http/Kernel.php

 protected $routeMiddleware = [
        ...
        'roleType' => \App\Http\Middleware\RoleType::class,
    ];

 

 

 Step 3: Add Route

Now, add route and add RoleType middleware in this route.

Route::get('check/role','UserController@checkRole')->middleware('roleType');

 

Step 4:  Add Controller

In this step, we will add the below code in UserController with the checkrole function.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function checkRole()
    {
        dd('checkRole');
    }
}

Now, it's time to run this example. so, copy the below link in your URL and get the output.

http://localhost:8000/check/role?type=user
http://localhost:8000/check/role?type=admin

 


You might also like:

Recommended Post
Featured Post
Laravel 9 Form Collective Example
Laravel 9 Form Collective Exam...

In this article, we will see laravel 9 form collective example. Here, we will learn how to use collective form and...

Read More

Jan-20-2023

How to Search Comma Separated Values in Laravel
How to Search Comma Separated...

Today, in this post i will show you how to search comma separated values in laravel. Here, we will find specific id from...

Read More

Sep-15-2021

How to Send Bulk Mail Using Queue in Laravel 8
How to Send Bulk Mail Using Qu...

In this article, we will see how to send bulk mail using a queue in laravel 8. Laravel queue is used for sending bu...

Read More

Feb-10-2021

How to Install Elasticsearch in Laravel 10
How to Install Elasticsearch i...

Hey developers! If you're diving into the world of Laravel 10 and looking to supercharge your application's...

Read More

Dec-25-2023