Reputation: 1019
We Have a Mobile App Rest API that has more than 70 routes and 12 controllers and folders structure look like,
/app
/Http
/Controllers
/Api
/v1
/UserController.php
/...other 11 controllers
/v2
And routes :
$router->group(['prefix' => 'Api/V1'], function () use ($router) {
$router->post('SignIn','Api\V1\UserController@SignIn');
$router->post('Verify','Api\V1\UserController@Verify');
..... other routes
});
After 8 months we need now to change some functions responses and add new functions to the UserController.php only (Other Controllers and routes have no updates) so my question here is how to create the V2 of the API in this situation?
Do we have to recreate all the unaffected other 11 Controllers and add the 70 routes to web.php under V2 prefix? or do I missing something here appreciate your help
Upvotes: 0
Views: 1024
Reputation: 40690
Typically the approach is the not have a v2 available for routes that are not in v2 yet so you'd do:
$router->group(['prefix' => 'Api/V1'], function () use ($router) {
$router->post('SignIn','Api\V1\UserController@SignIn');
$router->post('Verify','Api\V1\UserController@Verify');
..... other routes
});
$router->group(['prefix' => 'Api/V2'], function () use ($router) {
$router->post('SignIn','Api\V1\UserController@SignIn');
$router->post('Verify','Api\V1\UserController@Verify');
// And that's it
});
Here's what I would do for version management though:
Assume your reach a point where controllers exist in both Api/V1 to Api/V2 then you could do:
$allRoutes = function ($router) {
$router->post('SignIn','UserController@SignIn');
$router->post('Verify','UserController@Verify');
// all other routes.
}
Note there's no api/v1 or v2 here at all. You can then define your routes as:
$router->group([ 'prefix' => 'Api/V1', 'namespace' => 'App\Http\Controllers\Api\V1' ], function () use ($router) {
$allRoutes($router);
});
$router->group([ 'prefix' => 'Api/V2', 'namespace' => 'App\Http\Controllers\Api\V2' ], function () use ($router) {
$allRoutes($router);
});
This should replicate all routes in both v1 and v2.
However this does mean code duplication for routes that are not yet migrated to v2. The idea is that this will happen eventually.
Upvotes: 1