Getting route parameters in Lumen Getting route parameters in Lumen laravel laravel

Getting route parameters in Lumen


Lumen is so stripped down, that the Route resolves to a simple array, rather than a Route object.

This is a problem, since the Request::route($key) method assumes the Route will have a parameter method.

But if you call Request::route(null), the complete Route array will be returned, looking something like this:

array(3) {  [0]=>  int(1)  [1]=>  array(2) {    ["uses"]=>    string(40) "App\Http\Controllers\SomeController@index"    ["middleware"]=>    array(2) {      [0]=>      string(4) "auth"      [1]=>      string(4) "example"    }  }  [2]=>  array(1) {    ["id"]=>    string(36) "32bd15fe-fec8-11e7-ac6b-e0accb7a6476"  }}

Where [2] always seems to contain the Route parameters.

I made a simple helper class to work with the Route parameters on Lumen. You can get, set and forget route parameters. This is works great if you need to manipulate them in your middleware.

Save in app/Support/RouteParam.php: https://gist.github.com/westphalen/c3cd187007e0448bcb7fca1de091e4df

And simply use it like this: $id = RouteParam::get($request, 'id');

Blame illuminate/http/Request.php:

/** * Get the route handling the request. * * @param  string|null  $param * * @return \Illuminate\Routing\Route|object|string */public function route($param = null){    $route = call_user_func($this->getRouteResolver());    if (is_null($route) || is_null($param)) {        return $route;    }    return $route->parameter($param); // can't call parameter on array.}


The problem is Lumen just is not yet registers route information in the global middleware.

So, you'll just get empty string if you call $request->route():

class MyMiddleware {    public function handle($request, Closure $next)    {        $request->route(); // empty string or null        return $next($request);    }}

The decision is to define your middleware as a route middleware and manually set it to each needed route.bootstrap/app.php:

$app->middleware([//    MyMiddleware::class, // do not use global middleware]);$app->routeMiddleware([    MyMiddleware::class,]);// And then apply the middleware to every route using a group:$app->router->group([    'namespace' => 'App\Http\Controllers',    'middleware' => [MyMiddleware::class],], function ($router) {    require __DIR__ . '/../routes/web.php';});

The final correct middleware:

class MyMiddleware{    public function handle($request, Closure $next)    {        // @see https://github.com/laravel/lumen-framework/issues/119#issuecomment-298835011        $route = $request->route();        $routeParameters = is_array($route) ? $route[2] : $route->parameters();             return $next($request);    }}


I'm not sure where you are trying to access this, but if it's from the controller, the Lumen documentation explains that it's accessed by type-hinting it in the method: https://lumen.laravel.com/docs/master/requests#accessing-the-request

From the documentation:

You may still type-hint the Illuminate\Http\Request and access your route parameter id by defining your controller method like the following:

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class UserController extends Controller{    /**     * Update the specified user.     *     * @param  Request  $request     * @param  string  $id     * @return Response     */    public function update(Request $request, $id)    {        //    }}