How to Register Middleware in Laravel: Simple Guide
In Laravel, you register middleware by adding it to the
app/Http/Kernel.php file either in the $middleware array for global middleware or in the $routeMiddleware array for route-specific middleware. Then, you can apply it to routes or controllers using the middleware name.Syntax
Middleware registration in Laravel happens mainly in the app/Http/Kernel.php file. You add your middleware class to one of these arrays:
- $middleware: for global middleware that runs on every request.
- $routeMiddleware: for middleware you want to assign to specific routes by a key name.
Example syntax for route middleware registration:
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'custom' => \App\Http\Middleware\CustomMiddleware::class,
];After registering, use the key (like 'custom') in your routes or controllers.
php
protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'custom' => \App\Http\Middleware\CustomMiddleware::class, ];
Example
This example shows how to register a custom middleware and apply it to a route.
1. Create middleware with php artisan make:middleware CheckAge.
2. Register it in app/Http/Kernel.php under $routeMiddleware:
'check.age' => \App\Http\Middleware\CheckAge::class,3. Use it in routes:
Route::get('/profile', function () {
return 'Profile page';
})->middleware('check.age');php
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class CheckAge { public function handle(Request $request, Closure $next) { if ($request->age < 18) { return redirect('home'); } return $next($request); } } // In app/Http/Kernel.php protected $routeMiddleware = [ 'check.age' => \App\Http\Middleware\CheckAge::class, ]; // In routes/web.php Route::get('/profile', function () { return 'Profile page'; })->middleware('check.age');
Output
If request age < 18, user is redirected to 'home'; otherwise, 'Profile page' is shown.
Common Pitfalls
- Forgetting to register the middleware class in
Kernel.phpcauses Laravel to not recognize it. - Using the full class name instead of the middleware key in routes will not work.
- Not importing the middleware class correctly can cause errors.
- Applying global middleware when route-specific is intended can slow down requests unnecessarily.
php
<?php // Wrong: Using class name directly in route Route::get('/dashboard', function () { return 'Dashboard'; })->middleware(\App\Http\Middleware\Authenticate::class); // This will fail // Right: Use middleware key registered in Kernel.php Route::get('/dashboard', function () { return 'Dashboard'; })->middleware('auth');
Quick Reference
| Step | Action | File/Location |
|---|---|---|
| 1 | Create middleware class | Run: php artisan make:middleware YourMiddleware |
| 2 | Register middleware | app/Http/Kernel.php in $middleware or $routeMiddleware |
| 3 | Use middleware | Apply key in routes or controllers with ->middleware('key') |
| 4 | Test middleware | Visit route to check behavior |
Key Takeaways
Register middleware in app/Http/Kernel.php to make Laravel recognize it.
Use the middleware key from Kernel.php when applying middleware to routes.
Global middleware runs on every request; route middleware runs only where assigned.
Always create middleware classes with artisan for proper structure.
Test middleware by visiting routes to confirm expected behavior.