Table of Contents
遇到的情况
理解其中的困难
架构的目的及要求
微信群里的讨论
请求形式的约定
两种常见的管理代码的方式
git/svn 的 tag 管理方式
只有一个分支,在代码中根据版本信息做判断
我总结的解决办法
Home Backend Development PHP Tutorial PHP 开发的 API 多版本管理实践

PHP 开发的 API 多版本管理实践

Jun 20, 2016 pm 12:42 PM

遇到的情况

本文针对移动互联网客户端需要兼容旧版的情况,强制升级到最新版本的 app 不在讨论之列。

在 bugtags.com 项目中,我们的版本遵循下面规范。
1.0.1
大功能.小更新.bug 修正
我们的版本列表如下:

1.0、1.1、1.2、1.3、1.4
2.0、2.1、2.2、2.3
3.0、3.1

5.0

这样一个版本结构,所有版本都可以用,跨度最大时,1.0 用户要跟 5.0 用户并存。
以 /api/user/info 接口举例,经过这么多版本的迭代,版本 1.0 跟 3.0 的返回数据结构可能已经完全不同了。
对于这样一个系统,如何设计一个完备的版本架构非常重要。

理解其中的困难

移动互联网,有别于传统的 web 开发。其快速迭代、版本升级与传统的 web 开发相比,有如下困难:

  • 用户获取困难,留存率低

  • 客户端升级成本高,部分用户拒绝升级

  • 多个版本服务器端代码量大,急剧拉高维护成本

  • 架构的目的及要求

  • 简化版本管理流程,易配置管理

  • 缩小服务器端的 php 代码规模

  • 尽量不要引入新的要素

  • 微信群里的讨论

    请求形式的约定

  • 使用域名,如 v1.api.bugtags.com 来区分接口的版本

  • 将版本信息放到 url 的 pathinfo 中,如 api.bugtags.com/v1/

  • 将版本信息放到请求参数中,如 api.bugtags.com/user/1?_ver=1.0.1

  • 将版本信息放到 http header 中,如 API_VER: 1.0.1

  • 版本号用域名是比较不被认同的方案,主要原因是域名管理往往跨部门,增加了沟通成本。
    http 头是我个人最赞同的一种方式,可以保持 url 的整洁。
    url 参数中携带版本号的方式也很好,但是要注意不要跟业务逻辑的参数名重复。

    两种常见的管理代码的方式

    git/svn 的 tag 管理方式

    优点,随时切换分支成本低,尤其在 git 管理代码时。
    缺点,如果多个版本需要修改时,代码合并工作量大。

    只有一个分支,在代码中根据版本信息做判断

    优点,代码的总体规模小(只有一份代码)
    缺点,在需要判断版本的地方会有大量的分支语句

    我总结的解决办法

    最后的解决办法充分利用了 php 的 autoload 加载机制和命名空间。

  • 假设 base 是所有业务的基础,是第一个版本,也是生命周期最长的版本。

  • v10 对版本 1.x.x 提供服务,最大限度消除业务点上的版本逻辑判断,但是不绝对拒绝。

  • v20/v30 基于 v10 版本开发

  • v40 版本基于 v30 版本开发

  • 举例说明

    v10 提供 a,b,c 三个接口
    v20 提供 a1,b,c 三个接口, a1 是 a 的修改
    v30 提供 a,b1,c 三个接口, b1 是 b 的修改

    用下面三段代码来具体描述


    配置版本:

    • 基础目录 base 存放大部分公共代码

    • 版本目录 v10/v20 都是版本目录,里面存放此版本与 基础版本不同的逻辑

    • 版本区别以文件为最小粒度,以上面三段代码可以看出。

    用户要访问 /api/user/info?ver=3.0.1 此时,类的加载顺序依次为:

  • 在 v30 下尝试加载 Config.php 失败

  • 在 v10 下尝试加载 Config.php 失败

  • 在 Base 下尝试加载 Config.php 成功

  • 执行相关逻辑

  • 这是限制只能继承一层的原因是尽可能的降低系统的复杂度。这种方式管理代码已经在几个项目中得到一些验证。系统代码的复杂度可以很大程度上降低,尤其是多个版本迭代、又不能强制升级的系统中。另外需要注意的就是 :

  • 使用这个方式处理加载时,在经过几个版本的沉淀后,应该将通用部分渐渐沉淀到BASE版本中

  • 发布系统最好带有删除文件功能,否则被部分沉淀后,高版本会依旧使用高版本的代码。

  • 笔者在开发和运营 bugtags.com ,这是一款能够极大的提升 app 开发者测试效率的 SDK 产品,欢迎使用、转发推荐。

    我们团队长期求 PHP 后端,有兴趣请加下面公众号勾搭:


    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Chat Commands and How to Use Them
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

    JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

    Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

    Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

    Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

    Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

    How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

    Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

    Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

    The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

    Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

    The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

    How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

    Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

    See all articles