API versioning in Laravel App

API versioning in Laravel App

In this tutorial i will explain you to set up versioning in your laravel app or website. Its depends on your Application size, If you have a small application and few changes then you can use same controller with logics or different methods or method over loading. But you are going to develop large-scale of API application then you have to need separate controllers and methods for each version.

I'm going to create separate controllers for versions V1 and V2. You can create more versions based on your requirements i.e. V1, V2., V3, V4 and more... 

for version v1 controller is app\Http\Controllers\API\v1\APIController 
for version v2 controller is app\Http\Controllers\API\v2\APIController

In config\app.php define a new array variable versions, for this we will defined 2 versions becasue My App had launched 2 versions yet v1 and v2 

Add this in app.php 'versions' => ['v1','v2'] 

In api.php route file add group route for versions which we created in config\app.php

Also Read: Send transactional email with Sendgrid in Laravel
$arrayVersions = \config('app.versions');

foreach ($arrayVersions as $version) {

    Route::group(['prefix' => $version], function () use ($version) {
        //get controller path
        $controller = 'API\\'.$version.'\\APIController';
        Route::post('login', $controller.'@loginAuthentication');
        Route::post('get-user', $controller.'@getUser');
        Route::post('register', $controller.'@userRegister');
    });
}

Links will be

http://127.0.0.1:8000/{version}/login
http://127.0.0.1:8000/{version}/get-user
http://127.0.0.1:8000/{version}/register

Also Read: How to upload multiple images or files from frontend in WordPress without any plugin

In this tutorial i will explain you to set up versioning in your laravel app or website. Its depends on your Application size, If you have a small application and few changes then you can use same controller with logics or different methods or method over loading. But you are going to develop large-scale of API application then you have to need separate controllers and methods for each version.

I'm going to create separate controllers for versions V1 and V2. You can create more versions based on your requirements i.e. V1, V2., V3, V4 and more... 

for version v1 controller is app\Http\Controllers\API\v1\APIController 
for version v2 controller is app\Http\Controllers\API\v2\APIController

In config\app.php define a new array variable versions, for this we will defined 2 versions becasue My App had launched 2 versions yet v1 and v2 

Add this in app.php 'versions' => ['v1','v2'] 

In api.php route file add group route for versions which we created in config\app.php

Also Read: Send transactional email with Sendgrid in Laravel
$arrayVersions = \config('app.versions');

foreach ($arrayVersions as $version) {

    Route::group(['prefix' => $version], function () use ($version) {
        //get controller path
        $controller = 'API\\'.$version.'\\APIController';
        Route::post('login', $controller.'@loginAuthentication');
        Route::post('get-user', $controller.'@getUser');
        Route::post('register', $controller.'@userRegister');
    });
}

Links will be

http://127.0.0.1:8000/{version}/login
http://127.0.0.1:8000/{version}/get-user
http://127.0.0.1:8000/{version}/register

Also Read: How to upload multiple images or files from frontend in WordPress without any plugin

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Add your comment

Close