Just in case anyone is wondering. No, this attempt does not work. It throws "not-found" exceptions.


Still trying to figuring out how this can be implemented in Laravel >5.2 but could solved the issue. Anyone out there could implement it?


Hello guys,

I have started to build an API and the versioning is something that i have to take care of it form the initial stage. The API must be RESTful and for that reason the api version is has been included in the request headers. As tjoskar proposed in a previous old post you can create a middleware which will be responsible to handle the request and check the headers for the api version. The middleware must be placed under the global HTTP middleware stack in the Kernel. The problem here is how we can pass the api version to the appropriate route path keeping the routes autonomous.

What i have already done in my implementation which is working properly, i replaced the request path with a new one including the api version and then i copied the request. Then, the handle method of my middleware returns the new duplicated request which is updated with the new uri path.


//a copy of the request
$dupRequest = $request->duplicate();

return $next($dupRequest);

Any comments or suggestions?


Personally I'd use a factory class here, which I guess would end up somewhat similar to the "api version class" mentioned earlier.

Please sign in or create an account to participate in this conversation.