The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Best Way to Handle API Versioning with Slim

Noah Goodrich's Avatar

Noah Goodrich

10 May, 2013 04:57 PM

We've just started building a RESTful API with Slim and want to support versioning.

Our current plan is to build it into the url like api.site.com/v1/endpoint.

How would you recommend constructing the routes so that managing the different versions is sane?

Thanks.

  1. Support Staff 1 Posted by Andrew Smith on 10 May, 2013 05:05 PM

    Andrew Smith's Avatar

    Hi Noah,

    We have recently added in route group in the dev branch which will allow
    you to group your routes easily. You would have a base route looking like
    this.

    $app->group('/v1', function () use ($app) {
        $app->get('/endpoint1', function () use ($app) {
    
        });
    
        $app->get('/endpoint2', function () use ($app) {
    
        });
    });
    
  2. 2 Posted by Noah Goodrich on 10 May, 2013 05:24 PM

    Noah Goodrich's Avatar

    Is there a way to specify the group in the $app get(), post(), delete(), put() methods?

  3. Support Staff 3 Posted by Andrew Smith on 11 May, 2013 02:45 PM

    Andrew Smith's Avatar

    why would you need to do that? you can create group in group in group... but not group in get, post, put and delete.

  4. 4 Posted by Shimon Schwartz on 12 May, 2013 12:54 PM

    Shimon Schwartz 's Avatar

    Assuming that /v1/ will never change for this version of API it could be much cleaner to have v1 directory in the root, and bootstrap slim from within it. It seems to me that group is meant more for segregating parts of application logic within the same version, not break down app into separate logics.

  5. 5 Posted by Pushkar on 23 Jan, 2016 07:45 PM

    Pushkar's Avatar

    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
    $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 veriosn 22-29

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

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac