Why do you do this in a middleware? Do you want this to be adapted to multiple endpoints? If not I would just implement this as a static route and point it to the get method of my controller.
Jun 20, 2016
4
Level 5
Lumen override route parameter in middleware
I'm currently trying to implement a Middleware in Lumen that makes it so that if someone types in a URL like this:
/users/self
It will replace "self" with the current logged in users ID, so when it gets to the controller the $id parameter is equal to a real ID instead of "self". However, I'm having a really hard time figuring out how to replace "self" with the ID by the time it gets to the controller. I thought maybe something like this would work:
public function handle($request, Closure $next)
{
if ($request->route()[2]['id'] === 'self') {
$request->route()[2]['id'] = $this->getCurrentUsersId();
}
return $next($request);
}
However, it does not update the requests 'id' value like I hoped it would. Does anyone have any advice on this?
Please or to participate in this conversation.