gaiatools/content-accord
Composer 安装命令:
composer require gaiatools/content-accord
包简介
Laravel package for API versioning with composable strategies and generic negotiation dimensions
README 文档
README
Content Accord is a Laravel package for API versioning with composable strategies and a generic negotiation layer. It supports URI, header, and Accept header versioning behind a single fluent API and prepares your application for future negotiation dimensions (locale, format, tenant).
Features
- URI, custom header, or Accept header versioning
- Optional resolver chaining (try multiple strategies in order)
- Configurable missing-version behavior
- Per-route-group fallback when the requested version is missing
- Deprecation headers with sunset and documentation links
- Attribute-driven version metadata on controllers and methods
- Generic negotiation foundation for future dimensions
Requirements
- PHP 8.3+
- Laravel 12+ (Laravel 11 and 13 are supported via constraints)
Installation
composer require gaiatools/content-accord
Publish the configuration file:
php artisan vendor:publish --tag=content-accord-config
Configuration
The main configuration lives in config/content-accord.php under the versioning key.
Key settings:
dimensions: array of dimension services to negotiatestrategy:uri,header, oracceptresolver: custom resolver class/binding for versioningchain: array of strategies to try in ordermissing_strategy:reject,default,latest, orrequiredefault_version: used when missing strategy isdefaultfallback: global default for version fallbackversions: registered versions and deprecation metadata
Usage
Fluent Route Groups (Recommended)
Use Route::apiVersion() to declare versioned route groups. The URI prefix is
managed automatically based on your configured resolver strategy.
use Illuminate\Support\Facades\Route; Route::apiVersion('1') ->prefix('api') ->middleware(['content-accord.negotiate']) ->group(function () { Route::get('/users', [V1\UserController::class, 'index']); }); Route::apiVersion('2') ->prefix('api') ->middleware(['content-accord.negotiate']) ->group(function () { Route::get('/users', [V2\UserController::class, 'index']); });
With the URI strategy (default), the above registers at /api/v1/users and
/api/v2/users. With header or Accept strategies, both register at /api/users
and Content Accord selects the right route at dispatch time.
Deprecation metadata is a fluent chain:
Route::apiVersion('1') ->prefix('api') ->deprecated() ->sunsetDate('2026-03-01') ->deprecationLink('https://docs.example.com/v1-migration') ->middleware(['content-accord.negotiate']) ->group(function () { Route::get('/users', [V1\UserController::class, 'index']); });
Header Strategy
// config/content-accord.php 'versioning' => ['strategy' => 'header'],
Route::apiVersion('1') ->prefix('api') ->middleware(['content-accord.negotiate']) ->group(function () { Route::get('/users', [V1\UserController::class, 'index']); });
Requests:
GET /api/users Api-Version: 1
Accept Header Strategy
GET /api/users Accept: application/vnd.myapp.v1+json
Custom Dimensions and Resolvers
Override the negotiated dimensions or the resolver implementation:
use GaiaTools\ContentAccord\Dimensions\VersioningDimension; use App\Http\Negotiation\LocaleDimension; 'dimensions' => [ VersioningDimension::class, LocaleDimension::class, ], 'versioning' => [ 'resolver' => [ App\Http\Negotiation\CustomVersionResolver::class, GaiaTools\ContentAccord\Resolvers\Version\HeaderVersionResolver::class, ], ],
Register any custom dimensions/resolvers in the container so they can be resolved.
Missing Version Behavior
Configure what happens when a request has no version:
'missing_strategy' => 'default', 'default_version' => '1',
Fallback Behavior
Enable fallback globally or per group:
// config 'fallback' => false, // route group override Route::apiVersion('2') ->prefix('api') ->fallback() ->middleware(['content-accord.negotiate']) ->group(function () { Route::get('/users', [V2\UserController::class, 'index']); });
If a request targets v3 but only v2 exists for that endpoint, the v2 route will be selected when fallback is enabled.
Attributes
Add version metadata on controllers or methods:
use GaiaTools\ContentAccord\Attributes\ApiVersion; use GaiaTools\ContentAccord\Attributes\MapToVersion; #[ApiVersion('2')] class UserController { public function index() {} #[MapToVersion('2.1')] public function show() {} }
Method-level attributes take precedence over class-level attributes. Attribute versions override the group version in route metadata. Mismatches are logged in local/testing environments.
Deprecation Headers
Mark version groups as deprecated and optionally add sunset dates and docs links:
Route::apiVersion('1') ->prefix('api') ->deprecated() ->sunsetDate('2026-03-01') ->deprecationLink('https://docs.example.com/v1-migration') ->middleware(['content-accord.deprecate', 'content-accord.negotiate']) ->group(function () { Route::get('/users', [V1\UserController::class, 'index']); });
The Deprecation, Sunset, and Link headers are added automatically when deprecation metadata is present.
Accessing the Negotiated Version
Use the apiVersion() helper in controllers or anywhere after the negotiate
middleware has run:
use GaiaTools\ContentAccord\ValueObjects\ApiVersion; public function index(): JsonResponse { $version = apiVersion(); // ?ApiVersion }
Or inject NegotiatedContext directly:
use GaiaTools\ContentAccord\Http\NegotiatedContext; $version = app(NegotiatedContext::class)->get('version');
Testing Utilities
Use the testing helper to attach API versions to test requests:
use GaiaTools\ContentAccord\Testing\Concerns\InteractsWithApiVersion; class ExampleTest extends TestCase { use InteractsWithApiVersion; public function test_example() { $this->withApiVersion('2')->get('/api/users'); } }
The helper respects the configured strategy (URI, header, or Accept).
Artisan Command
List configured versions and route counts:
php artisan api:versions
License
MIT
gaiatools/content-accord 适用场景与选型建议
gaiatools/content-accord 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「content negotiation」 「laravel」 「api versioning」 「content-accord」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gaiatools/content-accord 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gaiatools/content-accord 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gaiatools/content-accord 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides content-negotiation using Accept* headers for BEAR.Sunday
A block that displays featured content - large image, title, description and link.
A bundle that creates REST Apis based on your current controllers actions
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
A PSR-7 compatible library for making CRUD API endpoints
Portal Options Links
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-22