Passing parameters to middleware in Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Passing Parameters to Middleware in Laravel
Body: In Laravel, middleware is a powerful tool for handling requests before the controller or route handler handles them. By default, when using middleware with routes, we have access to the route parameters through the `$request` variable. However, if you need to use middleware on different routes and require custom parameters, things can get a bit tricky.
To address this issue, Laravel offers several ways to pass parameters between your controllers/routes and middleware. This blog post will discuss four common methods for passing parameters to middleware in Laravel.
1. Using the Middleware's Parameter Binding Feature (Recommended):
The first solution is the most straightforward way, which uses the built-in parameter binding feature of Laravel. To use this method, you'll need to define a global variable at the top of your middleware class and pass it to the `handle()` function:
public $custom_id;
public function handle($request, Closure $next) {
$this->custom_id = $request->route('id');
return $next($request);
}
Now that you've defined the custom parameter `$custom_id`, you can use it within your middleware without any issue. However, this approach is limited to a single middleware class since the variable is set at the class level. For multiple middleware classes, you should opt for other methods.
2. Using Route Models:
Another option is using route models, which allows you to create an Eloquent model or a custom data structure for your resources and reference it via the controller method `with()`. Then, pass the parameter as part of the model's definition in the route/controller. To follow this method, modify your route definition:
Route::get("/user/{id}", ['middleware' => 'auth', function ($id) {
$user = User::find($id);
return view('profile', compact('user'));
}]);
Now, inside your middleware class, you can access the parameter within the model. This approach is a bit more advanced than our first method but ensures consistent data formats throughout your application:
public function handle($request, Closure $next) {
$user = $request->route('user'); // Access the Eloquent model with custom name 'user'
return $next($request);
}
3. Using Middleware Closures:
For more complex scenarios where you need to pass multiple parameters or want complete flexibility in your parameter passing process, you can use middleware closures. Here is how it works:
Route::get("/user/{id}", ['middleware' => 'auth', function ($id) {
return Closure::from(function ($request, $response, $next) use ($id) {
// Custom middleware logic with access to the parameter
return $next($request);
})->handle($request, $response, $next);
}]);
4. Using Laravel's Service Container:
Finally, if you need a more advanced approach and want to create custom dependencies for your middleware parameters, you can use Laravel's service container. This method requires creating a new binding in the `AppServiceProvider`, which is executed when your application starts. Here is an example:
/**
* Register any application services.
*/
public function register() {
$this->app->bind('customParameter', function ($app) {
return new CustomParameter(); // Return custom parameter class
});
}
Now, inside your middleware:
public function handle($request, Closure $next) {
$parameter = $this->app['customParameter']; // Access the bound instance of the custom parameter class
return $next($request);
}
In conclusion, there are multiple ways to pass parameters from routes to middleware in Laravel. Depending on your requirements and preferences, you can choose one of these methods: use the built-in parameter binding feature, route models, middleware closures, or Laravel's service container for more advanced scenarios. By understanding and implementing these techniques, you can effectively use parameters within your middleware classes, ensuring efficient and secure application architecture.