Passing parameters to middleware in Laravel

Passing parameters to middleware in Laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Passing Parameters to Middleware in Laravel

When you define a route like Route::get("/user/{id}", ['middleware' => 'auth', ...]), the $id segment is extracted from the URL and injected into your closure or controller method. However, that value does not automatically become available inside the middleware's handle method. By default, the middleware receives only the HTTP request object and a callable for the next step in the pipeline.

To use the route parameter within the middleware, you must explicitly pass it to the handler. This is done by defining the middleware group with an additional key-value pair: 'middleware' => ['auth', 'user.param']. In this syntax, user.param tells Laravel that the second item in the array is a custom parameter name (param) that should be passed as an argument to the middleware. The value associated with it (in this case, the route segment {id}) becomes available via $param inside the handle method.

Here is how you would register and call the middleware:

Route::get('/user/{id}', function ($request, $id) {
    // Your controller logic here
})->middleware(['auth', 'user.id']);

In this example, the 'user.id' key indicates that the route parameter id should be passed to any middleware registered under that group. Inside your middleware class:

public function handle($request, Closure $next, $id)
{
    // Now $id is available here
    return $next($request);
}

This approach keeps the route definition declarative while allowing the middleware to access specific data needed for authorization or logging. For instance, you might check whether a user has permission to view a specific profile:

public function handle($request, Closure $next, string $userId)
{
    if (!auth()->check()) {
        return redirect('/login');
    }

    // Assume we have an authenticated User model
    $user = auth()->user();

    // Optional validation logic using the route parameter
    if ($user->id !== (int)$userId) {
        abort(403, 'You do not have permission to view this profile.');
    }

    return $next($request);
}

A key trade-off with this pattern is that it couples the middleware logic tightly to a single route definition. If you reuse the same middleware across multiple routes with different parameter names or structures, you must adjust the registration each time. This can lead to duplication if not managed carefully.

Another consideration is performance. Passing extra parameters does not inherently add overhead, but ensuring that those parameters are used efficiently prevents unnecessary queries inside the handle method. For example, avoid calling expensive database lookups unless absolutely necessary.

If you need more complex parameter handling—such as passing multiple values or objects—you can pass an array: 'middleware' => ['auth', 'user.id,user.name']. Laravel will then bind both parameters to the middleware in order. However, this increases complexity and reduces readability. Stick to simple cases unless your architecture demands otherwise.

For teams working on larger applications, consider defining custom middleware groups in app/Http/Kernel.php or using service providers to centralize these configurations. This reduces scattering of logic across route files and makes testing easier.

If you're looking for more examples of advanced routing patterns or middleware customization, resources like https://laravelcompany.com offer detailed guides that walk through similar scenarios with production-ready code samples.

Our network