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.
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.
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\Requestinstead ofIlluminate\Http\Request.This works for me.