xurudragon/api-versioning-bundle
Composer 安装命令:
composer require xurudragon/api-versioning-bundle
包简介
Symfony APIVersioningBundle
关键字:
README 文档
README
Informations
This bundle provide you a way to handle multiple version of your api.
See Usage section to know how ise this bundle
Installation
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require xurudragon/api-versioning-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
If you are on Symfony 3.* look in the app/AppKernel.php file of your project:
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new XuruDragon\ApiVersioningBundle\XuruDragonApiVersioningBundle(), ); // ... } // ... }
If you are on Symfony 4.* look in the config/bundles.php file of your project:
<?php //config/bundles.php return [ // .... XuruDragon\ApiVersioningBundle\XuruDragonApiVersioningBundle::class => ['all' => true], ];
Step 3: Configuration
Nothing special to do here. Unless you want to change the header name sent to the api to processing versioning changes You can add theses entries into your config files :
xurudragon_versioning: # Header name that should be sent to the api to processing versioning changes header_name: X-Accept-Version #Header name
Usage
Introduction
Your API reponse must always match the latest version of your API.
To support multiple versions, you must for each version of your API that breaks the backward compatibility:
- create a change class inheriting from the following abstract class:
XuruDragon\ApiVersioningBundle\Changes\AbstractChanges - add an entry in the configuration file corresponding to the class created previously
we recommend to you the following structure:
- Create a folder named
ApiChanges - Name your classes
ApiChangesXYZ, withXfor theMajorversion,Yfor theMinorversion andZfor thePatchversion
We assume that you know the semantic versioning system
Create the Change Class
Here is the 0.9.0 class version, which assumes that your API version is at least 0.9.1 .
<?php // ../ApiChanges/ApiChanges090.php namespace Your\Bundle\ApiChanges; use XuruDragon\ApiVersioningBundle\Changes\AbstractChanges, class ApiChanges090 extends AbstractChanges { /** * Apply version changes for current request. * * @param array $data * * @return null|array */ public function apply(array $data = []) { return $data; } /** * Returns true if this version changes is supported for current request. * * @param array $data * * @return null|bool */ public function supports(array $data = []) { return true; } // ... }
You should now implement the apply and supports methods with your own logic.
supports should return true or false following if this version change support the actual data returned by your latest api version.
apply should do whatever you want to transform the actual data into an version that correspond to this api version. The returned array would be encoded in json and return to the api client that have make the request.
####Example Imagine that version 1.0.0 of your API adds an entry to the json output of one of the called services. This entry would be "location" within the following data array returned by your latest API version :
$data = [ 'results' => [ 'firstname' => 'John', 'lastname' => 'Doe', 'location' => '4, Privet Drive, Little Whinging, Surey', ], ];
You must then delete this data entry so that the requested version 0.9.0 matches what the calling client is waiting for.
You can do that by modifying the previously created class with the following code :
<?php // ../ApiChanges/ApiChanges090.php namespace Your\Bundle\ApiChanges; use XuruDragon\ApiVersioningBundle\Changes\AbstractChanges, /** * Class ApiChanges090. */ class ApiChanges090 extends AbstractChanges { /** * {@inheritdoc} */ public function apply(array $data = []) { //short example, but can be more more complex unset($data['results']['location']); return $data; } /** * {@inheritdoc} */ public function supports(array $data = []) { //check for the entry column "location" in the results sub-array of $data return isset($data['results']) && array_search('location', array_keys($data['results']), true); } // ... }
Of course here it's a pretty simple example of delete One entry, the processing could be more more complicated.
Register your version changes class in the configuration
# Array of version changes that breaks backward compatibility # The array should be `desc` ordered. # The newest version should be the first and the oldest the last one versions: # version 0.9.0 - { version_number: "0.9.0", changes_class: "Your\Bundle\ApiChanges\ApiChanges090"}
Here you are, no more configuration is required.
xurudragon/api-versioning-bundle 适用场景与选型建议
xurudragon/api-versioning-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「versioning」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xurudragon/api-versioning-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xurudragon/api-versioning-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xurudragon/api-versioning-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Versioning behaviour for eloquent models
A decent, standards-compliant, Semantic Versioning (SemVer) parser and library
A Laravel package for managing model drafts.
A PSR-7 compatible library for making CRUD API endpoints
Initiate versioning the database like code.
Automatically create git tag about your Laravel app version
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-28