承接 gaiatools/content-accord 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 negotiate
  • strategy: uri, header, or accept
  • resolver: custom resolver class/binding for versioning
  • chain: array of strategies to try in order
  • missing_strategy: reject, default, latest, or require
  • default_version: used when missing strategy is default
  • fallback: global default for version fallback
  • versions: 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 gaiatools/content-accord 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-22