api-tools/api-version-manager 问题修复 & 功能扩展

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

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

api-tools/api-version-manager

Composer 安装命令:

composer require api-tools/api-version-manager

包简介

Simplify Laravel API versioning with ease. No more separate controllers—just flexibility, fallbacks, and version-specific components.

README 文档

README

License: MIT

The Laravel API Version Manager Package streamlines the management of API endpoint versions in Laravel applications. This package empowers you to effortlessly handle API versioning, eliminating the necessity to create individual controllers for each version. Its design presents a flexible and efficient solution, enabling you to define fallback versions and effortlessly generate version-specific Requests and Resources.

Features

  • Fallback Versions: Define fallback versions for your API routes, ensuring smooth transitions and maintaining backward compatibility.
  • Single Controller: Eliminate the need to create separate controllers for each API version. Our package dynamically injects version-specific Request and Resource classes into your existing controller.
  • Effortless Versioning: Easily manage API versions through route configuration. The package automatically handles the resolution of version-specific components, streamlining your development process.

Installation

You can install the package via Composer:

composer require api-tools/api-version-manager

Configuration

Publish Config File

It is mandatory to publish the config file before using the package. You can publish the config file using the following command:

php artisan vendor:publish --tag=api-version-manager

Mandatory Configurations:

  • Define available versions array in config
  • Define default version version in config

Other possible configurations are:

  • app_http_namespace (default: App\Http) - Generally we use App\Http namespace to store all our requests & resources classes, but if you are using different namespace then you can define it here.
  • api_prefix (default: api) - API prefix for all versioned routes.
  • use_fallback_entity (default: true) - If you want to use fallback entity for all request & resource class then set this to true, otherwise set it to false. For example, you define SampleRequest in V1, and now you want to use same request in V2 then you can set this option to true and it will automatically use SampleRequest from V1.

Available Commands

Create a new versioned request

php artisan make:versioned-request {name}

Possible options:

  • --force: Overwrite the request if it already exists.

Create a new versioned resource

php artisan make:versioned-resource {name}

Possible options:

  • --collection: Create a resource collection instead of a single resource.
  • --force: Overwrite the resource if it already exists.

Usage

Example of api.php file:

Route::prefix('v1')->group(function () {
    Route::get('endpoint1', [SomeController::class, 'endpoint1']); // Available on v1 & v2 (Via default fallback)
    Route::get('endpoint2', [SomeController::class, 'endpoint2']); // Available on v1 & v2 (Via default fallback)
});
Route::prefix('v2')->group(function () {
    Route::get('new-endpoint', [SomeController::class, 'endpoint3']); // Available only on v2 
});

Example of SomeController.php file:

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Resources\Versioned\EndpointResource; // Mandatory to use versioned resource only. Don't use `App\Http\Resources\V1\EndpointResource` or `App\Http\Resources\V2\EndpointResource`
use App\Http\Requests\Versioned\NewEndpointRequest; // Mandatory to use versioned request only. Don't use `App\Http\Requests\V1\NewEndpointRequest` or `App\Http\Requests\V2\NewEndpointRequest`
use Illuminate\Http\Request;

class SomeController extends Controller
{
    public function endpoint1(Request $request)
    {
        return DashboardResource::item(['some' => 'data']); // Replacement of native `new DashboardResource(['some' => 'data'])` resource
    }
    
    public function endpoint1(Request $request)
    {
        return DashboardResource::collection(['some' => 'data']);
    }
    
    public function endpoint3(NewEndpointRequest $request)
    {
        return DashboardResource::item(['some' => 'data']);
    }
}

License

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

api-tools/api-version-manager 适用场景与选型建议

api-tools/api-version-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.34k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 01 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 api-tools/api-version-manager 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-09