定制 malek/laravel-api-versioning 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

malek/laravel-api-versioning

最新稳定版本:v1.0.0

Composer 安装命令:

composer require malek/laravel-api-versioning

包简介

Opinionated API versioning, deprecation lifecycle, usage tracking and alerts for Laravel

README 文档

README

Latest Version License PHP Version Laravel

A production-ready API Versioning & Deprecation Lifecycle Manager for Laravel.

Table of Contents

Why This Package?

Laravel does not provide structured API lifecycle management out of the box.

As APIs evolve, teams need:

  • Version isolation (/v1, /v2)
  • Safe deprecation workflows
  • Sunset communication
  • Client-aware usage tracking
  • Deprecated traffic monitoring

This package provides an opinionated and production-focused solution.

Features

API Versioning

  • Route prefix versioning
  • Header-based version resolution
  • Query parameter fallback
  • Version mismatch protection

Example:

use Malek\ApiVersioning\Facades\ApiRoute;

ApiRoute::version('v1')->group(function () {
    Route::get('/users', fn () => 'Users v1');
});

Access:

GET /v1/users

API Deprecation Management

Mark routes as deprecated:

ApiRoute::version('v2')
    ->deprecated('2026-01-01', 'Please migrate to v3')
    ->group(function () {
        Route::get('/legacy', fn () => 'Deprecated');
    });

Response headers:

Deprecation: true
Sunset: 2026-01-01
Warning: 299 - "Please migrate to v3"

Per-Client Usage Tracking

Tracks:

  • Version
  • Route
  • Client (API key or authenticated user)

Supports:

  • Header-based client resolution
  • Auth-based resolution
  • Anonymous fallback

Deprecated Usage Alerts

  • Configurable threshold

  • Time window monitoring

  • Critical log trigger

Installation

Requirements

  • PHP 8.1+
  • Laravel 10 or 11

Install:

composer require malek/laravel-api-versioning

Publish config:

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

Quick Example

use Malek\ApiVersioning\Facades\ApiRoute;

ApiRoute::version('v1')->group(function () {
    Route::get('/users', fn () => 'Users v1');
});

ApiRoute::version('v2')
    ->deprecated('2026-01-01', 'Upgrade to v3')
    ->group(function () {
        Route::get('/users', fn () => 'Users v2');
    });

Configuration

File:

config/api-versioning.php

Default Version

'default_version' => 'v1',

Version Resolver

'resolver' => [
    'header' => 'X-API-Version',
    'query'  => 'api_version',
],

Client Resolver

'client' => [
    'resolver' => 'header',
    'header'   => 'X-API-KEY',
    'fallback' => 'anonymous',
],

Usage Tracking

'usage' => [
    'enabled' => true,
    'ttl' => 60 * 60 * 24 * 30,
],

Deprecation Alerts

'alerts' => [
    'enabled' => true,
    'threshold' => 100,
    'window_minutes' => 5,
],

Architecture

Request
  ↓
ResolveApiVersion Middleware
  ↓
VersionedRouteRegistrar
  ↓
DeprecationHeaders Middleware
  ↓
ApiUsageTracker
  ↓
DeprecatedUsageMonitor
  ↓
Response

Artisan Commands

php artisan api:versions
php artisan api:deprecated
php artisan api:usage
php artisan api:alerts

Use Cases

  • Mobile API version governance
  • SaaS public APIs
  • Gradual API migrations
  • Enterprise API lifecycle control
  • B2B multi-client APIs

Production Notes

  • Uses Laravel Cache (Redis recommended)

  • Logging-based alert mechanism

  • No database dependency

  • Horizontal scaling friendly

Testing

vendor/bin/phpunit

Roadmap

  • Database persistence driver
  • Slack / email alert integration
  • Prometheus metrics exporter
  • OpenAPI compatibility
  • Sunset enforcement mode

Admin dashboard

Contributing

  • Fork repository
  • Create feature branch
  • Submit Pull Request

License

MIT License

Author

Malek Deghlawi
GitHub: https://github.com/malek-deghlawi

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固