jeromejhipolito/laravel-api-versioning
Composer 安装命令:
composer require jeromejhipolito/laravel-api-versioning
包简介
Header-based API versioning with version flags for Laravel. Supports semantic versioning and feature flag-like version control.
README 文档
README
Header-based API versioning with version flags for Laravel. Supports semantic versioning and feature flag-like version control.
Features
- 🏷️ Header-based versioning - Uses
X-API-Versionheader (industry standard like Stripe, GitHub) - 📦 Semantic versioning - Full support for major.minor.patch format (1.0.0, 1.1.0, 2.0.0)
- 🚩 Version flags - Enable/disable versions independently (perfect for app store review periods)
- 🔒 Minimum version middleware - Require minimum version per route:
min.version:1.1.0 - 🔄 Controller inheritance - Override only changed methods in versioned controllers
- 📱 Mobile-friendly - Disabled versions return
version_enabled: falseso apps can hide features - 🌍 Translations - Built-in support for English, Japanese, and Korean
Installation
composer require jeromejhipolito/laravel-api-versioning
Publish Configuration (Optional)
php artisan vendor:publish --tag=api-versioning-config php artisan vendor:publish --tag=api-versioning-lang
Configuration
Add to your .env:
# Optional: Comma-separated list of enabled versions (null = all enabled) API_ENABLED_VERSIONS=1.0.0,1.1.0 # Optional: Default version when header is missing API_DEFAULT_VERSION=1.0.0
Edit config/api-versioning.php:
return [ 'supported_versions' => ['1.0.0', '1.1.0', '2.0.0'], 'enabled_versions' => env('API_ENABLED_VERSIONS') ? array_map('trim', explode(',', env('API_ENABLED_VERSIONS'))) : null, // null = all supported versions enabled 'default_version' => env('API_DEFAULT_VERSION', '1.0.0'), ];
Register Middleware
In bootstrap/app.php:
use JeromeJHipolito\ApiVersioning\Middleware\ApiVersionMiddleware; use JeromeJHipolito\ApiVersioning\Middleware\ResolveVersionedController; ->withMiddleware(function (Middleware $middleware) { $middleware->api(append: [ ApiVersionMiddleware::class, ResolveVersionedController::class, ]); })
Usage
Making Requests
# With version header curl -H "X-API-Version: 1.0.0" https://api.example.com/users # Without header (uses default version) curl https://api.example.com/users
Response Headers
Every response includes:
X-API-Version: 1.0.0X-API-Version-Enabled: trueX-API-Supported-Versions: 1.0.0, 1.1.0, 2.0.0X-API-Enabled-Versions: 1.0.0, 1.1.0
Check Version Status
GET /api/version/status
{
"status": "success",
"data": {
"current_version": "1.0.0",
"version_enabled": true,
"default_version": "1.0.0",
"supported_versions": ["1.0.0", "1.1.0", "2.0.0"],
"enabled_versions": ["1.0.0", "1.1.0"],
"version_flags": {
"1.0.0": true,
"1.1.0": true,
"2.0.0": false
}
}
}
Using in Controllers
use JeromeJHipolito\ApiVersioning\Traits\VersionAwareTrait; class UserController extends Controller { use VersionAwareTrait; public function show($id) { $user = User::find($id); // Check version if ($this->isVersionAtLeast('2.0.0')) { return new V2\UserResource($user); } return new UserResource($user); } }
Using in Resources
use JeromeJHipolito\ApiVersioning\Traits\VersionAwareResourceTrait; class UserResource extends JsonResource { use VersionAwareResourceTrait; public function toArray($request): array { return [ 'id' => $this->id, 'name' => $this->name, // Only in 1.1.0+ ...$this->mergeWhenVersion('1.1.0', [ 'profile_score' => $this->profile_score, ]), // Only below 2.0.0 (deprecated) ...$this->mergeWhenVersionBelow('2.0.0', [ 'legacy_field' => $this->old_data, ]), ]; } }
Disabled Version Response
When a version is supported but not enabled:
{
"status": "success",
"version_enabled": false,
"message": "This API version is currently disabled...",
"current_version": "2.0.0",
"data": null
}
This allows mobile apps to check version_enabled and hide features accordingly.
Version Flags Workflow
-
Add new version as supported but disabled
'supported_versions' => ['1.0.0', '2.0.0'],
API_ENABLED_VERSIONS=1.0.0
-
Deploy to production - Old apps continue working
-
Submit new app version - App checks
version_flagsand hides 2.0.0 features -
After app store approval - Enable the version
API_ENABLED_VERSIONS=1.0.0,2.0.0
Minimum Version Middleware
Require a minimum API version for specific routes or groups:
Register the Middleware Alias
In bootstrap/app.php:
use JeromeJHipolito\ApiVersioning\Middleware\MinimumVersionMiddleware; ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ 'min.version' => MinimumVersionMiddleware::class, ]); })
Usage
// Single route Route::post('new-feature', [FeatureController::class, 'store']) ->middleware('min.version:1.1.0'); // Route group Route::group(['middleware' => ['min.version:2.0.0']], function () { Route::post('advanced', [AdvancedController::class, 'store']); Route::delete('advanced/{id}', [AdvancedController::class, 'destroy']); });
Response When Version Is Too Low
{
"message": "This endpoint requires API version 1.1.0 or higher",
"current_version": "1.0.0",
"minimum_version": "1.1.0"
}
HTTP Status: 400 Bad Request
Available Methods
VersionAwareTrait (Controllers)
| Method | Description |
|---|---|
getApiVersion() |
Get current version string |
getApiMajorVersion() |
Get major version number |
getApiMinorVersion() |
Get minor version number |
getApiPatchVersion() |
Get patch version number |
isVersionAtLeast($v) |
Check if >= version |
isVersionBelow($v) |
Check if < version |
isVersionExactly($v) |
Check if exact version |
isVersionBetween($min, $max) |
Check if in range |
VersionAwareResourceTrait (Resources)
| Method | Description |
|---|---|
mergeWhenVersion($v, $array) |
Merge array if >= version |
mergeWhenVersionBelow($v, $array) |
Merge array if < version |
mergeWhenVersionExactly($v, $array) |
Merge array if exact version |
mergeWhenVersionBetween($min, $max, $array) |
Merge if in range |
whenVersion($v, $value, $default) |
Return value if >= version |
whenVersionBelow($v, $value, $default) |
Return value if < version |
License
MIT License. See LICENSE for details.
jeromejhipolito/laravel-api-versioning 适用场景与选型建议
jeromejhipolito/laravel-api-versioning 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「version」 「header」 「semver」 「versioning」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jeromejhipolito/laravel-api-versioning 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeromejhipolito/laravel-api-versioning 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jeromejhipolito/laravel-api-versioning 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Versioning behaviour for eloquent models
Converts SemVer version (like Composer packages) into integer version with operators. Helps managing versions: store, compare, sort and retrive by conditions.
Display a version number
PHP Version Manager for Windows CLI
A PSR-7 compatible library for making CRUD API endpoints
Obtains the latest version release of Composer Packages from the Packagist API
统计信息
- 总下载量: 120
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-20