承接 ejunker/laravel-api-evolution 相关项目开发

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

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

ejunker/laravel-api-evolution

Composer 安装命令:

composer require ejunker/laravel-api-evolution

包简介

Evolve your API while maintaining backwards compatibility. API versioning like Stripe.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel API Evolution is an API versioning library based on the idea of API evolution. It provides a way to make changes to your API way while maintaining backwards compatibility.

Inspired by Stripe's API versioning strategy. Users specify the desired version in a header and the request and response data will be modified to match the requested version.

Installation

You can install the package via composer:

composer require ejunker/laravel-api-evolution

You can run the installation command with:

php artisan api-evolution:install

The api-evolution:install command will create the config/api-evolution.php config file.

You will need to add the middleware to your api middleware group in app/Http/Kernel.php or to the group/route that you want.

'api' => [
    // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
    'throttle:api',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Ejunker\LaravelApiEvolution\ApiEvolutionMiddleware::class,
],

Usage

You can create an API migration using the command:

php artisan make:api-migration FirstLastName

This will create the file app/Http/Migrations/FirstLastName.php that you can edit to make the necessary changes to the request and/or response. Then you need to add it to the config/api-evolution.php file so that it runs when an old version is requested. The migrations listed for a version are the migrations needed to make it match the previous version.

API Migrations

Each API migration file has several properties and methods that you can use:

  • routeNames - an array of route names that this migration will run for. You can use wildcards such as api.v1.users.*
  • isApplicable() - if routeNames does not give you enough flexibility, you can use this method to determine if the migration should run based on the Request
  • migrateRequest() - allows you to modify the Request so that it is compatible with the newer version
  • migrateResponse() - allows you to modify the Response so that it is the older version that was requested

In addition to routeNames and isApplicable(), you can also specify the route names for a migration in the config file.

'versions' => [
    '2022-10-10' => [
        new \App\Http\Migrations\FirstLastName(['api.v1.users.show']),
    ],

    '2022-10-05' => [
        // first version
    ],
],

Binds

While API Migrations allow you to modify the Request and Response, sometimes it may be easier to use an entirely different FormRequest, JsonResource, or response Transformer. In those cases you can use a Bind to bind a different version into the container.

In the config/api-evolution.php

'versions' => [
    '2022-10-10' => [
        \App\Http\Migrations\FirstLastName::class
        new \Ejunker\LaravelApiEvolution\Bind(
            \App\Transformers\UserTransformer::class,
            \App\Transformers\UserTransformer_20221005::class,
        ),
    ],

    '2022-10-05' => [
        // first version
    ],
],

In this example, if the user requested the 2022-10-05 version it would apply the Bind which would bind the old version of the file into the container so that it would be used. If the user requested the latest version 2022-10-10 then the Bind would not run and the latest version of the file would be used.

Determine if a Migration Is Active

If you need to modify things other than Request/Response such as modifying a SQL query based on the version then you can use ApiEvolution::isActive(). For example, to know if the FirstLastName migration is active: ApiEvolution::isActive(FirstLastName::class)

Response Headers

Several headers are added to the response:

  • API-Version - the version requested or the latest version if no specific version is requested
  • API-Version-Latest - if the version requested is not the latest version then this header will be added
  • Deprecated - if there are migrations or Binds that run for this endpoint then this header will be added to indicate that there is a newer version of this endpoint

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

This package is based on the following:

License

The MIT License (MIT). Please see License File for more information.

ejunker/laravel-api-evolution 适用场景与选型建议

ejunker/laravel-api-evolution 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 370 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「versioning」 「laravel」 「ejunker」 「laravel-api-evolution」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ejunker/laravel-api-evolution 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ejunker/laravel-api-evolution 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-11