In the platform I'm developing it's possible to request the user's company data by sending a GET request to the api/company/{id}
API endpoint.
By default the id
parameter is an integer but usually it's also possible to set it as a string: api/company/mine
will retrieve the authenticated user's company data.
In order to allow this I created a middleware that intercepts the API call and replaces mine
with the actual company ID.
Unfortunately, my solution is not exactly what I had in mind.
Here's my current solution:
$request->merge([
'id' => $request->user()->company
]);
This works by adding the id to the request's input so that it can be accessed later on using $request->input('id');
, but the problem is that if I try to access $request->route('id')
I still get the old value.
Is it possible to change the route parameter directly?
P.S.
Another solution that comes in my mind is to actually programmatically create a new request with the new parameter and then pass that one to the next()
function in the middleware.