Created
May 23, 2022 15:39
-
-
Save 5818959/f423db8394943e60063293c34ce6873c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple Laravel 8+ versioning | |
Based on [this article](https://dev.to/dalelantowork/laravel-8-api-versioning-4e8) that contains outdated information. | |
We will have endpoints like this: | |
``` | |
https://site.test/api/v1/hello | |
https://site.test/api/v2/hello | |
``` | |
## Folders organization | |
``` | |
/app | |
/controllers | |
/Api | |
/V1 | |
/HelloController.php | |
/V2 | |
/HelloController.php | |
``` | |
## Setup loading routes according to the version in URL | |
Open `app/Providers/RouteServiceProvider.php` and add attribute: | |
``` | |
/** | |
* The controller namespace for the versioning API application. | |
* | |
* @var string|null | |
*/ | |
protected $apiNamespace = 'App\\Http\\Controllers\Api'; | |
``` | |
Then edit `boot` method: | |
``` | |
$this->routes(function () { | |
// ... | |
Route::prefix('api/v1') | |
->middleware(['api', 'api_version:v1']) | |
->namespace("{$this->apiNamespace}\V1") | |
->group(base_path('routes/api_v1.php')); | |
Route::prefix('api/v2') | |
->middleware(['api', 'api_version:v2']) | |
->namespace("{$this->apiNamespace}\V2") | |
->group(base_path('routes/api_v2.php')); | |
}); | |
``` | |
## Routes file for each version | |
Create routes file for each version we have. | |
### First version routes (v1): | |
``` | |
vi routes/api_v1.php | |
``` | |
Add the following code: | |
``` | |
<?php | |
use App\Http\Controllers\Api\V1\WelcomeController; | |
use Illuminate\Support\Facades\Route; | |
Route::get('hello', [HelloController::class, 'index'])->name('api.hello'); | |
``` | |
Second version routes (v2): | |
``` | |
vi routes/api_v2.php | |
``` | |
Add the following code: | |
``` | |
<?php | |
use App\Http\Controllers\Api\V2\WelcomeController; | |
use Illuminate\Support\Facades\Route; | |
Route::get('hello', [HelloController::class, 'index'])->name('api.hello'); | |
``` | |
## Create controllers | |
``` | |
php artisan make:controller Api/V1/HelloController.php | |
php artisan make:controller Api/V2/HelloController.php | |
``` | |
Add `index` method to `Api/V1/HelloController.php`: | |
``` | |
public function index() | |
{ | |
$response = [ | |
'success' => true, | |
'message' => 'Welcome to API version 1.', | |
]; | |
return response()->json($response, 200); | |
} | |
``` | |
Add `index` method to `Api/V2/HelloController.php`: | |
``` | |
public function index() | |
{ | |
$response = [ | |
'success' => true, | |
'message' => 'Welcome to API version 2.', | |
]; | |
return response()->json($response, 200); | |
} | |
``` | |
## (optional) Requested Api version in config | |
We can get requested API version in app by add middleware. | |
``` | |
php artisan make:middleware APIVersion | |
``` | |
Open the file `app/Http/Middleware/APIVersion.php` and add the code: | |
``` | |
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Http\Request; | |
class APIVersion | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next | |
* @param string $version | |
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse | |
*/ | |
public function handle(Request $request, Closure $next, $version) | |
{ | |
config(['app.api.version' => $version]); | |
return $next($request); | |
} | |
} | |
``` | |
Register it on `app/Http/Kernel.php`: | |
``` | |
protected $routeMiddleware = [ | |
// ... | |
'api_version' => App\Http\Middleware\APIversion::class, | |
]; | |
``` | |
Now we can get requested version inside the controller: | |
``` | |
$response = [ | |
'success' => true, | |
'message' => 'Welcome to API version ' . config('app.api.version'), | |
]; | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment