承接 timopaul/laravel-api-versioning 相关项目开发

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

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

timopaul/laravel-api-versioning

Composer 安装命令:

composer require timopaul/laravel-api-versioning

包简介

Provides simple functions for using API versioning in Laravel via a header parameter.

README 文档

README

Latest Version Total Downloads License

Introduction

timopaul/laravel-api-versioning is a Laravel package that allows you to handle API versioning seamlessly by passing the version as a header parameter. It simplifies the process of managing multiple API versions while maintaining clean and structured code.

Features

  • Accepts API version via a custom header parameter.
  • Easy integration with existing Laravel applications.
  • Customizable versioning behavior through configuration.
  • Middleware for handling API version validation.

Installation

You can install the package via Composer:

composer require timopaul/laravel-api-versioning

Configuration

After installation, you can publish the configuration file to customize the package behavior:

php artisan vendor:publish --tag="api-versioning-config"

This will create a configuration file located at config/api-versioning.php. You can modify this file according to your requirements.

The default configuration file looks like this:

declare(strict_types=1);

return [
    /*
    |--------------------------------------------------------------------------
    | API Version Header Key
    |--------------------------------------------------------------------------
    |
    | The HTTP header that contains the API version. This value is used in the
    | middleware to determine the requested version.
    |
    */
    'header_key' => 'X-API-Version',

    /*
    |--------------------------------------------------------------------------
    | Default API Version
    |--------------------------------------------------------------------------
    |
    | If no API version is specified (e.g. for internal requests), this value
    | can be used as the default version. If `null` is specified here, the API
    | version is a required header parameter for every request.
    |
    */
    'default_version' => '1.0',

    /*
    |--------------------------------------------------------------------------
    | Default API Versions Enumeration
    |--------------------------------------------------------------------------
    |
    | Enumeration to define all possible versions of the API with identifier
    | and name.
    |
    */
    'enumeration_class' => \TimoPaul\LaravelApiVersioning\Enums\ApiVersion::class,

];

Managing Versions with Enums

The package uses an enumeration to define and manage API versions. This approach ensures consistency and type safety throughout your application. The ApiVersion enum is structured as follows:

declare(strict_types=1);

namespace TimoPaul\LaravelApiVersioning\Enums;

use TimoPaul\LaravelApiVersioning\Interfaces\ApiVersionInterface;
use TimoPaul\LaravelApiVersioning\Traits\ApiVersionEnumerationHelpers;

enum ApiVersion: string implements ApiVersionInterface
{
    use ApiVersionEnumerationHelpers;

    case v100 = '1.0.0';
    case v101 = '1.0.1';
    case v123 = '1.2.3';
    case v200b = '2.0.0 BETA';
}

Using this enum, you can easily reference specific API versions without hardcoding strings. For example, when defining routes or applying middleware:

use Timopaul\LaravelApiVersioning\Enums\ApiVersion;
use Illuminate\Support\Facades\Route;

Route::middleware(['api', 'api.version:' . ApiVersion::V1->value])->group(function () {
    Route::get('/example', [ExampleController::class, 'index']);
});

This centralized version management improves code maintainability and reduces errors.

Usage

Middleware

The package provides a middleware to handle API versioning. To enable it, add it to your route or global middleware stack:

// In routes/api.php
use Illuminate\Support\Facades\Route;

Route::middleware(['api-versioning'])->group(function () {
    Route::get('/example', [ExampleController::class, 'index']);
});

Accessing the API Version

You can access the requested API version in your controller or service using:

$version = app('api-version');

This will return the version passed in the API-Version header or the default version if none was provided.

Example Request

curl -X GET \
  -H "API-Version: v2" \
  https://your-api-domain.com/example

Testing

Run the package tests to ensure everything works as expected:

composer test

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository from GitLab.
  2. Create a new branch for your feature or bugfix.
  3. Submit a pull request with a detailed description of your changes.

License

This package is open-sourced software licensed under the MIT License.

For more information, visit the GitLab repository.

timopaul/laravel-api-versioning 适用场景与选型建议

timopaul/laravel-api-versioning 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-02