Trying to read route parameters from my Middleware class with $request->route(‘id’). This gives me the error that it can’t call parameter() on an array. When I look at the source code, the $this->getRouteResolver() returns an array. Is this a bug?
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);
}
}
Yes, I guess it is a bug.
It's not a bug that it returns an array, but then the fact we try to treat it as an object is a bug.
Currently using $routeParams = $request->route()[2]; and $routeParams['paramName'] to retrieve a route parameter from the request. I'm not sure if the index [2] will be always the same tho. Haven't digged that much into the source code, but it seems it does.
I can create a PR later as a proposal on how we could fix this issue if you want to @GrahamCampbell.
This issue has been around for quite sometime (Since Lumen's release actually!) and there has never been a fix to it till date.
Ref: #198 #119
I had even sent a PR because i was having this very same issue back then (Though it's just a helper which never made it to the core): #52
I actually ended up writing a new package with that helper here.
I'm once again having the same issue, this time with a third-party package which uses $request->route() to access the param.
@GrahamCampbell Will there ever be a fix for this :?
Will there ever be a fix for this :?
We're open to PRs.
Alright, I'll create a new PR with a solution soon then or if @guidsen is doing, that would also be good too.
Sent a PR: https://github.com/laravel/framework/pull/11873
Please review. Tested both in Lumen and Laravel, Works good.
P.S We could also add support for the default value, isn't it? Since the parameter() method in Laravel Route class already supports it but it isn't added in the request's route method.
@taylorotwell @GrahamCampbell Since the PR in Laravel was rejected because it should extend the request class in Lumen, Should i go ahead and do that or wait for such thing happening? Lmk.
why longtime?
@irazasyed @taylorotwell @GrahamCampbell
Hope to solve as soon as possible
@SPRING-Z The PR I had sent got closed. They don't fix the issue nor accept the PR. So no hope!
So, I made a PR for this using the method (I think) that @crynobone and @taylorotwell suggested on the laravel PR submitted by @irazasyed. Feedback would be appreciated.
https://github.com/laravel/lumen-framework/pull/580
The thing that got me the most was this: on the response Closures I had to change the type hinting to Laravel\Lumen\Http\Response from Illuminate\Http\Response to get the functionality to work, even though the Lumen response is a subclass of the Laravel response. I was getting a valid object with the Illuminate type hint, but it didn't have the correct information in it. Did I miss something?
My PR above also addresses https://github.com/laravel/lumen-framework/issues/119 which is where with my google search trying to figure out why this was breaking when I used $request->route('id')
@killermonk Looked good but it seems it got rejected. Try sending one to master branch? Because 5.4 was already released and it makes sense to not modify it right now.
@irazasyed yeah, I had no idea what Taylor's terse response meant. This message from you makes it make sense. I'll go ahead and cherry pick everything over to master and get a new PR in.
@irazasyed @SPRING-Z in other news, the work around for this in lumen is pretty easy:
$request->route('id') becomes $request->route()[2]['id'].
Maybe the easier thing to do is just to make sure the docs get updated some how for lumen?
@killermonk I ended up making larasupport package almost 2 years ago because of this issue and I have a route_parameter() helper to deal with this.
But still, if it can make it to the core, that would be good ofc.
Workaround docs won't be good, though.
Should be fixed in 5.7. Feel free to reply if you're still experiencing it in 5.7.
Most helpful comment
Should be fixed in 5.7. Feel free to reply if you're still experiencing it in 5.7.