ravibastola's avatar

Manage versioning in an api.

How to manage different versions of an api with Lumen? An approach may be to manage different routes pointing to different controllers but that seems way too redundant. Copying existing code and placing it to versions does not seem fit, Please share if anybody has done this differently.

0 likes
6 replies
Sergiu17's avatar
Routes::get('/api/v1/users', function() {});

Routes::get('/api/v2/users', function() {});

That's how people do, they version it with v1, v2, vn prefix

ravibastola's avatar

I have exactly asked the question, I cannot figure out where is your answer pointing towards, I want to dynamically match the version and use controller based on that.

jimmitjoo's avatar

I would still probably go that way tho.

But if you don't want that, this is a pretty hacky, but potential, solution?

Route::get('/api/{version}/users', function($version)

    $app = app();
    $controller = $app->make('\App\Http\Controllers\'.$version.'\UsersController');
    return $controller->callAction('index', $parameters = []);

);
Sergiu17's avatar

Manage versions in an API and Match the versions completely two different things

Good luck :)

ravibastola's avatar

@Sergiu17 By managing i meant i did not want to copy the v1/Controllers to v2/Controllers to use multiple versions of an api, does not that seem redoing the same thing, what do you suggest?

mbpcoder's avatar

Recently I was very annoying with copying all the routes for v2. so I created a package to fallback versions. if a route is created for v1 and it didn't change you don't need to create v2 for it. it just automatically fallback to v1 https://github.com/mbpcoder/laravel-api-versioning but it only works with Laravel. base on the Idea you could write a Lumen version.

Please or to participate in this conversation.