Laravel 9 Create Custom Middleware Example

In this article, we will see laravel 9 create a custom middleware example. Here, we will learn about how to create middleware in laravel 9 and how to use 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 how to create custom middleware in laravel 9, laravel 9 custom middleware.

In this example, we have created "roleType" middleware and we 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 as below in the demo link.



Step 1: Create Middleware

In this step, we will create custom middleware. So, run the following command in the terminal and create a middleware.

php artisan make:middleware RoleType



After running the above command, RoleType.php is created.



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 Middleware on Kernel File

Now, we have to register role type middleware on the kernel file.


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


 Step 3: Add Route

Now, add the route and add RoleType middleware in routes. Also, you can add middleware to the group of routes. 


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!



Step 4:  Add Controller

In this step, we will create UserController with a checkRole function.


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
    public function checkRole()



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



You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.