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
$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-userhttp://127.0.0.1:8000/{version}/
register
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
$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-userhttp://127.0.0.1:8000/{version}/
register
Recommanded Articles
- How to create a multilevel category and subcategory in Laravel
- How to check YouTube video exist Laravel validation
- Multiple user roles authentication Laravel 8
- Deploy Laravel project from local to production server
- Make custom pagination URL in Laravel without query strings
- Web Scraping in Laravel using Goutte
- Insert values during migration run laravel
- Validation for string characters only with custom message in Laravel
- Add new columns in a table Laravel
- How to create foreign key constraints in Laravel
Latest Comments