Lumen-framework: Request:route() method throws exception

Created on 16 Sep 2016  路  6Comments  路  Source: laravel/lumen-framework

Hello,

This Request method throws an exception:

    /**
     * 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;
        } else {
            return $route->parameter($param);
        }
    }

In Lumen, the route resolver returns an array, and so the function call fails. I would have submitted a pull request but I'm not sure how you might want this problem solved, since the Request class is part of Laravel.

As a slight addendum, I discovered this while creating a Lumen friendly FormRequest. Is this something you guys might be interested in a pull request for? It has its own service provider, so it's an optional thing that might be nice for other developers.

Most helpful comment

If you're using lumen (5.7 and above), I figured out a good solution to solve this:
If you created a custom request class, just make it extends: Laravel\Lumen\Http\Request instead of Illuminate\Http\Request.

This works for me.

All 6 comments

I opened a proposal to create a common route interface back in April but I haven't received a response. I may just submit a PR and see what happens.

Same issue here, any news on that @taylorotwell ? Did you guys found a workaround ?

If you check the the issue I linked it doesn't sound like this is going to be fixed :(

You can probably workaround it with a route middleware that replaces the array with an object, or by extending the service container binding. I don't think lumen fires an event when the route is matched.

// in a service provider somewhere
$this->app->resolving(Request::class, function ($request) {
    if (!is_array($request->route())) {
        return;
    }
    $route = Route::fromArray($request->route());
    $request->setRouteResolver(function () use ($route) {
        return $route;
    });
});


class Route
{
    private $parameters;

    public function __construct(array $parameters)
    {
        $this->parameters = $parameters;
    }

    public static function fromArray(array $route)
    {
        return new self($route[2]); // 0 is the Dispatcher::FOUND code, 1 is the action, 2 is the params.
    }

    public function parameter($name, $default = null)
    {
        return array_get($this->parameters, $name, $default);
    }
}

This is fixed in 5.7.

@JacksonIV thanks for checking 馃憤

If you're using lumen (5.7 and above), I figured out a good solution to solve this:
If you created a custom request class, just make it extends: Laravel\Lumen\Http\Request instead of Illuminate\Http\Request.

This works for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhengyuhe123 picture zhengyuhe123  路  4Comments

jampack picture jampack  路  3Comments

jairobjunior picture jairobjunior  路  4Comments

georgeboot picture georgeboot  路  4Comments

rtheunissen picture rtheunissen  路  3Comments