Lumen: how can I get url parameters from middleware?

This is mine routes.php

:

$app->get('/users/{id}/', ['middleware' => 'example', function () {
  return "users";
}]);

      

This is a feature handle

in middleware:

public function handle($request, Closure $next)
{
  // I would like to get the value of the url parameter {id} here

  return $next($request);
}

      

Is there a way to get the parameter id

from my middleware?

* Change *

I am using Lumen 5.1.0 .

+3


source to share


2 answers


Laravel has several common ways to work with Lumen. And getting the parameter URI in middleware is one of them. In Laravel I just need to make a call $request->id

, it will work like magic. But here, to get the parameter in Lumen, I need to do something like this:



$request->route()[2]['id']

      

+5


source


If the value passed in $request

is an instance Illuminate\Http\Request

I think maybe this class has a method called input()

that allows you to do just that:

enter image description here



You should try the following:

$id = $request->input('id');

      

+1


source







All Articles