How to Create Middleware in Laravel: Step-by-Step Guide
In Laravel, create middleware using the
php artisan make:middleware MiddlewareName command, then define your logic in the handle method. Register the middleware in app/Http/Kernel.php to apply it globally or to specific routes.Syntax
Middleware in Laravel is a PHP class with a handle method that receives the request and a closure to pass the request further. You create it with Artisan, then add your logic inside handle. Finally, register it in the Kernel to use it.
php artisan make:middleware MiddlewareName: creates the middleware file.handle(Request $request, Closure $next): method where you write your filtering logic.- Return
$next($request)to continue the request cycle.
bash/php
php artisan make:middleware CheckAge // In app/Http/Middleware/CheckAge.php public function handle(Request $request, Closure $next) { if ($request->input('age') <= 18) { return redirect('home'); } return $next($request); }
Example
This example creates a middleware named CheckAge that blocks users under 18 by redirecting them to the home page. It shows how to create, write, and register middleware.
php
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class CheckAge { public function handle(Request $request, Closure $next) { if ($request->input('age') <= 18) { return redirect('home'); } return $next($request); } } // Register in app/Http/Kernel.php protected $routeMiddleware = [ 'check.age' => \App\Http\Middleware\CheckAge::class, ]; // Use in routes/web.php Route::get('/profile', function () { return 'Welcome to your profile!'; })->middleware('check.age');
Output
If user age <= 18, browser redirects to /home; otherwise, shows 'Welcome to your profile!'
Common Pitfalls
- Forgetting to register the middleware in
Kernel.phpmeans it won't run. - Not returning
$next($request)stops the request and causes errors. - Using middleware on routes without proper parameters can cause unexpected redirects or failures.
- Middleware logic should be simple and fast to avoid slowing down requests.
php
<?php // Wrong: Missing return statement public function handle(Request $request, Closure $next) { if ($request->input('age') <= 18) { redirect('home'); // Missing return } return $next($request); } // Right: public function handle(Request $request, Closure $next) { if ($request->input('age') <= 18) { return redirect('home'); } return $next($request); }
Quick Reference
Summary tips for creating middleware in Laravel:
- Use
php artisan make:middleware Nameto create. - Write logic inside
handle(Request $request, Closure $next). - Always return
$next($request)to continue. - Register middleware in
app/Http/Kernel.phpunder$middlewareor$routeMiddleware. - Apply middleware to routes or groups using
->middleware('name').
Key Takeaways
Create middleware with Artisan and write logic in the handle method.
Always return $next($request) to continue the request cycle.
Register middleware in Kernel.php to enable its use.
Apply middleware to routes or globally as needed.
Keep middleware logic simple and efficient.