30

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?

2
  • 5
    you have $request variable use it :) ($request->id) Commented Dec 5, 2016 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. Commented Jan 14, 2021 at 11:44

3 Answers 3

53

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)

Sign up to request clarification or add additional context in comments.

3 Comments

At least add the source: mattstauffer.com/blog/…
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?
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'])
40

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.

Comments

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);

    }
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.