Search code examples
phplaravellaravel-5laravel-5.1middleware

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.


Solution

  • 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
    }