mnarbash/api-versioning-by-header-request 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mnarbash/api-versioning-by-header-request

Composer 安装命令:

composer require mnarbash/api-versioning-by-header-request

包简介

A package for managing API versioning in Laravel using request headers

README 文档

README

Latest Version on Packagist Software License Total Downloads

This package provides middleware for managing API versioning in Laravel using request headers.

Features

  • You Not Need To Change Your Routes Endpoints. You can use the same endpoints for all versions of your API.

  • API versioning by header request: This package allows you to version your API by adding the API-VERSION header to the request. You can specify the supported API versions and the default API version in the configuration file.

  • App version checking: This package includes a middleware that checks the APP-VERSION header in the request against the supported app versions and the minimum supported app version specified in the configuration file. If the app version is not supported, the middleware returns an error response to update app.

  • Customizable folder structure: You can specify a custom folder structure for the API versions in the configuration file. This allows you to organize your controllers and routes in a way that makes sense for your application.

  • Api Versioning base on your controller classes: The package allows you to version your API by multi controller classes in your route define.

Youtube Video

Check out this video to see how to use this package in your project.

Laravel API Versioning By Header Request

https://youtu.be/2HeFCJieVLo

Installation

To install the package, run the following command:

composer require mnarbash/api-versioning-by-header-request

Configuration

Add ServiceProvider to config/app.php in providers section:

Mnarbash\ApiVersioningByHeaderRequest\ApiVerServiceProvider::class,

To configure the package, publish the configuration file using the following command:

php artisan vendor:publish --provider="Mnarbash\ApiVersioningByHeaderRequest\ApiVerServiceProvider" --tag=config

This will create a config/api-versioning.php file in your project. You can modify the following options in this file:

  • check_app_version_support: This option tells the package whether to check the app version support. If set to true, the package will check the app version and return an error response if the app version is not supported.

  • api_versioning_enabled: This option tells the package whether to enable API versioning. If set to true, the package will check the API-VERSION header in the request and use it to determine the version of the API to be used.

  • default_api_version: This option specifies the default API version to be used when the API-VERSION header is not present in the request.

  • not_do_anything_when_version_is_not_set: This option tells the package whether to do anything when the version is not set. If set to true, the package will not change the controller name when the version is not set.

  • default_app_version: This option specifies the default app version to be used when the APP-VERSION header is not present in the request.

  • min_supported_app_version: This option specifies the minimum supported app version. If the app version in the request is less than this version, return an error response.

  • update_urls: This option specifies the URLs to show update app.

  • not_support_response_status_code: this option specifies the status code of the response when the app version is not supported.

To use the check api and redirect to new controller, apply it to a route as follows:

-Note: Add the newer controller in the first of array. We use this order to check the version and redirect to the older controller if version is not exist. Example: If you have 2 controller with version 1 and 2 and you want to redirect to version 1 if version 2 is not exist,

    Route::get('testVer', ApiVersioning::UseApiMultiVersions([
        'V1' => [V1TestApiController::class, 'testVer'],
        'V0' => [TestApiController::class, 'testVer'], 
        '0' => [TestApiController::class, 'testVer'] //set default version if not set in header
    ]))

Full Example For Route File

Route::prefix('test')->group( function () {
    Route::get('func', ApiVersioning::UseApiMultiVersions(
        [
            '0'=> function () {
                return 'version 0 or default version';
            },
            'V1' => function () {
                return 'v1';
            },
            'V2' => function () {
                return 'v2';
            },
            'V3' => function () {
                return 'v3';
            },
            'V5' => function () {
                return 'v5';
            },
        ]
    ));

    Route::get('controller', ApiVersioning::UseApiMultiVersions(
        [
            '0'=> [V0TestController::class, 'index'],
            'V1' =>  [V1TestController::class, 'index'],
            'V2' =>  [V2TestController::class, 'index'],
            'V3' =>  [V3TestController::class, 'index'],
            'V5' =>  [V5TestController::class, 'index'],
        ]
    ));
    Route::resource('res', ApiVersioning::UseApiMultiVersions(
        [
            '0'=> ResourceController::class,
            'V2'=> V2ResourceController::class,
        ]
    ));
});

Also you can get api version from anywhere in your code like this:

    $apiVersion = ApiVersioning::getApiVersion();

The package includes the CheckAppVersion middleware:

  • CheckAppVersion: This middleware checks the app version in the APP-VERSION request header. If the version is not supported, it returns an error.

  • to use the check app middleware, apply it to a api $middlewareGroups in app/Http/Kernel file as follows:

    use Mnarbash\ApiVersioningByHeaderRequest\Middleware\CheckAppVersion;

    protected $middlewareGroups = [
        'api' => [
        // ...
                  CheckAppVersion::class,
        ],
    ];

License

This package is licensed under the MIT license. See the LICENSE file for more information.

mnarbash/api-versioning-by-header-request 适用场景与选型建议

mnarbash/api-versioning-by-header-request 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 68 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 mnarbash/api-versioning-by-header-request 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-06