简体   繁体   中英

How to pass URL parameter from route to middleware?

If I have middleware like:

<?php namespace App\Http\Middleware;

class SomeMiddleware
{
    public function handle($request, Closure $next, $id = null)
    {
        //
    }
}

In kernel.php:

'someMiddleware'    => \App\Http\Middleware\SomeMiddleware::class,

In routes.php :

Route::put('post/{id}', ['middleware' => 'someMiddleware']);

How I can pass id captured in {id} to my middleware? I know that I can pass some custom parameter like this:

Route::put('post/{id}', ['middleware' => 'someMiddleware:16']);

But in laravel documentation there is no described how to pass argument captured in route pattern.

I think that you can get the parameter from inside the middleware like this:

//your middleware's method  
public function handle($request, Closure $next) 
{
    //get the ID
    $id = $request->id
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM