diff --git a/README.md b/README.md index 4bc50e5..683793a 100644 --- a/README.md +++ b/README.md @@ -230,7 +230,9 @@ laravel-api-kit/ │ ├── sanctum.php # Token auth config │ └── scramble.php # API docs config ├── routes/ -│ └── api.php # API routes with versioning +│ ├── api.php # API routes entry point +│ └── api/ +│ └── v1.php # Version 1 routes ├── tests/ │ └── Feature/Api/V1/ │ └── AuthTest.php # Authentication tests @@ -241,7 +243,7 @@ laravel-api-kit/ ## API Versioning -This kit uses [grazulex/laravel-apiroute](https://github.com/Grazulex/laravel-apiroute) for API versioning with support for: +This kit uses [grazulex/laravel-apiroute](https://github.com/Grazulex/laravel-apiroute) v2.x for API versioning with support for: - **URI Path** (default): `/api/v1/users`, `/api/v2/users` - **Header**: `X-API-Version: 2` @@ -252,22 +254,34 @@ This kit uses [grazulex/laravel-apiroute](https://github.com/Grazulex/laravel-ap 1. Create controllers in `app/Http/Controllers/Api/V2/` 2. Create requests in `app/Http/Requests/Api/V2/` -3. Update `routes/api.php`: +3. Create route file `routes/api/v2.php`: ```php -use Grazulex\ApiRoute\Facades\ApiRoute; +current(); +use App\Http\Controllers\Api\V2\AuthController; +use Illuminate\Support\Facades\Route; -// Version 1 - Mark as deprecated -ApiRoute::version('v1', function () { - Route::post('register', [V1\AuthController::class, 'register']); - // ... existing routes -})->deprecated('2025-06-01')->sunset('2025-12-01'); +Route::post('register', [AuthController::class, 'register']); +// ... more routes +``` + +4. Update `config/apiroute.php`: + +```php +'versions' => [ + 'v1' => [ + 'routes' => base_path('routes/api/v1.php'), + 'status' => 'deprecated', + 'deprecated_at' => '2025-06-01', + 'sunset_at' => '2025-12-01', + 'successor' => 'v2', + ], + 'v2' => [ + 'routes' => base_path('routes/api/v2.php'), + 'status' => 'active', + ], +], ``` ### Deprecation Headers @@ -275,8 +289,8 @@ ApiRoute::version('v1', function () { When accessing deprecated versions, responses include RFC-compliant headers: ```http -Deprecation: @1717200000 -Sunset: Sun, 01 Dec 2025 00:00:00 GMT +Deprecation: Sun, 01 Jun 2025 00:00:00 GMT +Sunset: Mon, 01 Dec 2025 00:00:00 GMT Link: ; rel="successor-version" ``` @@ -574,14 +588,11 @@ class PostResource extends JsonResource 4. **Add Routes:** ```php -// routes/api.php -ApiRoute::version('v1', function () { +// routes/api/v1.php +Route::middleware('auth:sanctum')->group(function () { // ... existing routes - - Route::middleware('auth:sanctum')->group(function () { - Route::apiResource('posts', PostController::class); - }); -})->current(); + Route::apiResource('posts', PostController::class); +}); ``` 5. **Create Tests:** diff --git a/composer.json b/composer.json index 8001109..de3c4e1 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": "^8.2", "dedoc/scramble": "^0.12", - "grazulex/laravel-apiroute": "^1.2", + "grazulex/laravel-apiroute": "^2.0", "laravel/framework": "^12.0", "laravel/sanctum": "^4.0", "laravel/tinker": "^2.10.1", diff --git a/config/apiroute.php b/config/apiroute.php index 99d9434..abadeae 100644 --- a/config/apiroute.php +++ b/config/apiroute.php @@ -3,6 +3,28 @@ declare(strict_types=1); return [ + /* + |-------------------------------------------------------------------------- + | API Versions + |-------------------------------------------------------------------------- + | + | Define your API versions here. Each version has its own route file, + | middleware, and lifecycle status. + | + */ + 'versions' => [ + 'v1' => [ + 'routes' => base_path('routes/api/v1.php'), + 'middleware' => [], + 'status' => 'active', + 'deprecated_at' => null, + 'sunset_at' => null, + 'successor' => null, + 'documentation' => null, + 'rate_limit' => null, + ], + ], + /* |-------------------------------------------------------------------------- | Detection Strategy diff --git a/routes/api.php b/routes/api.php index 62f4324..c09e1e7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,29 +1,18 @@ name('api.v1.register'); - Route::post('login', [AuthController::class, 'login'])->name('api.v1.login'); - - // Protected routes - Route::middleware('auth:sanctum')->group(function () { - Route::post('logout', [AuthController::class, 'logout'])->name('api.v1.logout'); - Route::get('me', [AuthController::class, 'me'])->name('api.v1.me'); - }); -})->current(); +// Routes are now loaded automatically from config/apiroute.php +// See routes/api/v1.php for version 1 routes diff --git a/routes/api/v1.php b/routes/api/v1.php new file mode 100644 index 0000000..f0d0012 --- /dev/null +++ b/routes/api/v1.php @@ -0,0 +1,23 @@ +name('api.v1.register'); +Route::post('login', [AuthController::class, 'login'])->name('api.v1.login'); + +// Protected routes +Route::middleware('auth:sanctum')->group(function () { + Route::post('logout', [AuthController::class, 'logout'])->name('api.v1.logout'); + Route::get('me', [AuthController::class, 'me'])->name('api.v1.me'); +});