24

Let's say I have a route pointing to middleware;

Route::get("/user/{id}", ['middleware' => 'auth', function ($id) {

}]);

And my middleware code is as follows:

public function handle($request, Closure $next)
{
    return $next($request);
}

If I want to use $id in the middleware, how do I do that?

smottt
  • 3,272
  • 11
  • 37
  • 44
athene
  • 408
  • 1
  • 6
  • 15
  • 5
    you have `$request` variable use it :) (`$request->id`) – Kyslik Dec 05 '16 at 14:20
  • 2
    A very misleading title, it should be: "Passing route parameters to middleware in Laravel". Perhaps one would like to pass a parameter that is not a route parameter. The accepted answer does not match the title. – Silidrone Jan 14 '21 at 11:44

3 Answers3

46

In you case you cannot pass $id into the middleware.

Generally you can pass parameters to middleware via using : symbol like this:

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

And get the passed parameter into middleware method like this:

<?php

namespace App\Http\Middleware;

use Closure;

class Authentication
{
    public function handle($request, Closure $next, $role)
    {
        if (auth()->check() && auth()->user()->hasRole($role)) {
            return $next($request);
        }

        return redirect('login');
    }
}

Note that the handle() method, which usually only takes a $request and a $next closure, has a third parameter, which is our middleware parameter.

If you passed in multiple parameters like auth:owner,subscription to your middleware call in the route definition, just add more parameters to your handle method which will look like this - handle($request, Closure $next, $role,$subscription)

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45
  • 9
    At least add the source: https://mattstauffer.com/blog/passing-parameters-to-middleware-in-laravel-5.1/ – Marcello Davi Apr 07 '20 at 16:09
  • okay, but from where will the parameter values be taken? Do you have to add them into the request via controller, do you have to add the to the URL like "some/{parameter}/store" or how will it be filled? – Adam Bajger May 23 '21 at 13:53
  • 1
    Also might be useful for someone, that u can pass multiple parameters to the middleware, using the comma separator. Like this : `Route::middleware(['auth.basic:driverName,fieldName'])` – priMo-ex3m Aug 05 '21 at 09:27
34

You can use one of the following method to access the route parameter in a middleware:

First Method

$request->route()->parameters();

This method will return an array of all the parameters.

Second Method

$request->route('parameter_name');

Here parameter_name refers to what you called the parameter in the route.

Amit Gupta
  • 17,072
  • 4
  • 41
  • 53
0

On this I move the auth()->check() to be handled by the middleware the would format the code to be something like

<?php

namespace App\Http\Middleware;

use Closure;

class Authentication
{
    public function handle($request, Closure $next, $role)
    {
        if (!auth()->user()->hasRole($role)) {
            return redirect('login');
        }
           return $next($request);

    }
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 17 '23 at 06:47