How to Pass Parameter to Middleware in Laravel Easily
In Laravel, you can pass parameters to middleware by adding them after the middleware name in the route definition using a colon and commas, like
middleware('auth:api'). Inside the middleware's handle method, you receive these parameters as additional arguments after the request and next callback.Syntax
To pass parameters to middleware in Laravel, use the following syntax in your route or controller:
middleware('middlewareName:param1,param2')— parameters are separated by commas.- In the middleware class, the
handlemethod accepts these parameters after$requestand$next.
php
Route::get('/profile', function () { // Your code here })->middleware('role:admin,editor'); // Middleware handle method public function handle($request, Closure $next, ...$roles) { // $roles will be ['admin', 'editor'] return $next($request); }
Example
This example shows a middleware that checks if the user has one of the allowed roles passed as parameters.
php
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class CheckRole { public function handle(Request $request, Closure $next, ...$roles) { $user = $request->user(); if (!$user || !in_array($user->role, $roles)) { return response('Unauthorized.', 403); } return $next($request); } } // In routes/web.php Route::get('/dashboard', function () { return 'Welcome to dashboard'; })->middleware('checkrole:admin,manager');
Output
If user role is 'admin' or 'manager', the page shows 'Welcome to dashboard'. Otherwise, it returns 'Unauthorized.' with status 403.
Common Pitfalls
- Forgetting to register the middleware alias in
app/Http/Kernel.phpcauses Laravel to not find your middleware. - Not using
...$paramsin thehandlemethod will miss parameters passed. - Passing parameters without commas or with spaces can cause parsing errors.
- Trying to pass complex objects instead of strings or simple values is not supported in route middleware parameters.
php
/* Wrong: Missing spread operator for parameters */ public function handle($request, Closure $next, $role) { // This only accepts one parameter, not multiple } /* Right: Use spread operator to accept multiple parameters */ public function handle($request, Closure $next, ...$roles) { // Now $roles is an array of all passed parameters }
Quick Reference
| Step | Description | Example |
|---|---|---|
| 1 | Define middleware with parameters in route | ->middleware('role:admin,editor') |
| 2 | Accept parameters in middleware handle method | public function handle($request, Closure $next, ...$roles) |
| 3 | Use parameters inside middleware logic | if (!in_array($user->role, $roles)) { return response('Unauthorized.', 403); } |
| 4 | Register middleware alias in Kernel.php | 'checkrole' => \App\Http\Middleware\CheckRole::class |
Key Takeaways
Pass parameters to middleware by adding them after the middleware name separated by commas.
Use the spread operator (...$params) in the middleware handle method to receive all parameters.
Always register your middleware alias in app/Http/Kernel.php to use it in routes.
Parameters passed to middleware must be simple strings or values, not complex objects.
Check your parameter syntax carefully to avoid parsing errors in route definitions.