3

We've been using Slim framework to create REST APIs for a mobile app, and right now the index.php resides inside a folder v1/ inside the web root. This index.php contains all the APIs.

However, with newer versions of mobile app requiring some slight different response for same api end point, we need API versioning which can be easy to scale. We don't want to use different folders (like v2, v3, ...) each having its index.php.

Ideally, there should "probably" be just a single index.php file, and lets say there's a requirement to add a new API version vX, which has changes may be in just one of the common APIs, I'd like the code changes to be minimal, may be restricted only to that API function, and may be a few else where (to enable this new version for all the other APIs).

Is there a simple and short way to achieve this such that its easy to add new versions? How should this generally be done?

EDIT 1:

The following is a "potential" solution to fix this problem, please feel free to point to issues in it, and better solutions:

I've chosen to use the conditions (application-wide Route conditions) to achieve this:

<?php
\Slim\Route::setDefaultConditions(array(
    'apiVersion' => '[1-29]'       /* indicates integer API version between 1 and 29 */
));

Any API which supports API versions 1-29 (exactly same processing)

$app = new \Slim\Slim();
$app->get('/:apiVersion/API_A/:lastName', $callableA)
    ->conditions(array('lastName' => '[a-z]{10,}'));

An API which is different for version 1-21 and different for version 22-29

$app->get('/:apiVersion/API_B, $callableB1)
    ->conditions(array('apiVersion' => '[1-21]'));
$app->get('/:apiVersion/API_B, $callableB2)
    ->conditions(array('apiVersion' => '[22-29]'));

So if I've say 100s of APIs, and I need to make changes to 3 APIs for a new version 30 of API, then I can simply change those three APIs as done above for API_B, and change the application wide route condition for apiVersion.

Ouroboros
  • 1,432
  • 1
  • 19
  • 41

0 Answers0