How To Create Custom Middleware In Laravel 9

Websolutionstuff | Mar-27-2022 | Categories : Laravel

In this article, we will see how to create custom middleware in laravel 9. Laravel middleware provides a convenient mechanism for inspecting and filtering HTTP requests entering your application. For example, 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 your application's login screen. if the user is authenticated, the middleware will allow the request to proceed further into the application. If your projects have multiple users then we need to use middleware to provide different access or login to different users.

So, let's see laravel 9 custom middleware,  custom middleware laravel 9, how to create middleware in laravel 9, laravel middleware example.

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

In this step, we will 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 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('You do not have access!!');
        }

        return $next($request);
    }
}

 

Step 2 : Register This Middleware on Kernel File

Now, 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. Also, you can add middleware to the group of routes. 

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

 

Step 4 :  Add Controller

In this step, we will create UserController with a 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 copy the below link in your URL and get output.

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

 


You might also like :

Recommended Post
Featured Post
How to Uninstall Composer on Ubuntu 23.04
How to Uninstall Composer on U...

Hey there! If you've found your way to this guide, chances are you're looking to bid farewell to Composer on you...

Read More

Jan-22-2024

Laravel 9 whereNull / whereNotNull Query Example
Laravel 9 whereNull / whereNot...

In this article, we will see the laravel 9 whereNull / whereNotNull query example. In laravel 9 additional where clauses...

Read More

Oct-18-2022

How to Create Newsletter in Laravel 10
How to Create Newsletter in La...

Hey there! In this article, I'm thrilled to guide you through the exciting world of creating newsletters in Laravel...

Read More

Nov-29-2023

How To Add Watermark On Image In Laravel 10
How To Add Watermark On Image...

In this article, we will how to add a watermark to an image in laravel 10. Here we will learn about laravel 10 adding a...

Read More

Mar-31-2023