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.
$request->server->set('REQUEST_URI',str_replace('api/','/api/'.$apiNamespace.'/',$request->path())); //a copy of the request $dupRequest = $request->duplicate(); return $next($dupRequest);
Any comments or suggestions?