# 版本
你的API應該是版本化的。不像你完全控制在客戶端和服務器端Web應用程序代碼, 對于API,您通常沒有對API的客戶端代碼的控制權。 因此,應該盡可能的保持向后兼容性(BC),如果一些不能向后兼容的變化必須引入 APIs,你應該增加版本號。你可以參考[Semantic Versioning](http://semver.org/)?有關設計的API的版本號的詳細信息。
關于如何實現API版本,一個常見的做法是在API的URL中嵌入版本號。 例如,`http://example.com/v1/users`代表`/users`版本1的API. 另一種API版本化的方法最近用的非常多的是把版本號放入HTTP請求頭,通常是通過`Accept`頭, 如下:
~~~
// 通過參數
Accept: application/json; version=v1
// 通過vendor的內容類型
Accept: application/vnd.company.myapp-v1+json
~~~
這兩種方法都有優點和缺點, 而且關于他們也有很多爭論。 下面我們描述在一種API版本混合了這兩種方法的一個實用的策略:
* 把每個主要版本的API實現在一個單獨的模塊ID的主版本號 (例如?`v1`,?`v2`)。 自然,API的url將包含主要的版本號。
* 在每一個主要版本 (在相應的模塊),使用?`Accept`?HTTP 請求頭 確定小版本號編寫條件代碼來響應相應的次要版本.
為每個模塊提供一個主要版本, 它應該包括資源類和控制器類 為特定服務版本。 更好的分離代碼, 你可以保存一組通用的 基礎資源和控制器類, 并用在每個子類版本模塊。 在子類中, 實現具體的代碼例如?`Model::fields()`。
你的代碼可以類似于如下的方法組織起來:
~~~
api/
common/
controllers/
UserController.php
PostController.php
models/
User.php
Post.php
modules/
v1/
controllers/
UserController.php
PostController.php
models/
User.php
Post.php
v2/
controllers/
UserController.php
PostController.php
models/
User.php
Post.php
~~~
你的應用程序配置應該這樣:
~~~
return [
'modules' => [
'v1' => [
'basePath' => '@app/modules/v1',
],
'v2' => [
'basePath' => '@app/modules/v2',
],
],
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['v1/user', 'v1/post']],
['class' => 'yii\rest\UrlRule', 'controller' => ['v2/user', 'v2/post']],
],
],
],
];
~~~
因此,`http://example.com/v1/users`將返回版本1的用戶列表,而?`http://example.com/v2/users`將返回版本2的用戶。
使用模塊, 將不同版本的代碼隔離。 通過共用基類和其他類 跨模塊重用代碼也是有可能的。
為了處理次要版本號, 可以利用內容協商 功能通過 yii\filters\ContentNegotiator 提供的行為。`contentNegotiator`?行為可設置 yii\web\Response::acceptParams 屬性當它確定 支持哪些內容類型時。
例如, 如果一個請求通過?`Accept: application/json; version=v1`被發送, 內容交涉后,yii\web\Response::acceptParams將包含值`['version' => 'v1']`.
基于?`acceptParams`?的版本信息,你可以寫條件代碼 如 actions,resource classes,serializers等等。
由于次要版本需要保持向后兼容性,希望你的代碼不會有 太多的版本檢查。否則,有機會你可能需要創建一個新的主要版本。
- 介紹(Introduction)
- 關于 Yii(About Yii)
- 從 Yii 1.1 升級(Upgrading from Version 1.1)
- 入門(Getting Started)
- 安裝 Yii(Installing Yii)
- 運行應用(Running Applications)
- 第一次問候(Saying Hello)
- 使用 Forms(Working with Forms)
- 玩轉 Databases(Working with Databases)
- 用 Gii 生成代碼(Generating Code with Gii)
- 更上一層樓(Looking Ahead)
- 應用結構(Application Structure)
- 結構概述(Overview)
- 入口腳本(Entry Scripts)
- 應用(Applications)
- 應用組件(Application Components)
- 控制器(Controllers)
- 模型(Models)
- 視圖(Views)
- 模塊(Modules)
- 過濾器(Filters)
- 小部件(Widgets)
- 前端資源(Assets)
- 擴展(Extensions)
- 請求處理(Handling Requests)
- 運行概述(Overview)
- 引導(Bootstrapping)
- 路由引導與創建 URL(Routing and URL Creation)
- 請求(Requests)
- 響應(Responses)
- Sessions and Cookies
- 錯誤處理(Handling Errors)
- 日志(Logging)
- 關鍵概念(Key Concepts)
- 組件(Components)
- 屬性(Properties)
- 事件(Events)
- 行為(Behaviors)
- 配置(Configurations)
- 別名(Aliases)
- 類自動加載(Class Autoloading)
- 服務定位器(Service Locator)
- 依賴注入容器(Dependency Injection Container)
- 配合數據庫工作(Working with Databases)
- 數據庫訪問(Data Access Objects): 數據庫連接、基本查詢、事務和模式操作
- 查詢生成器(Query Builder): 使用簡單抽象層查詢數據庫
- 活動記錄(Active Record): 活動記錄對象關系映射(ORM),檢索和操作記錄、定義關聯關系
- 數據庫遷移(Migrations): 在團體開發中對你的數據庫使用版本控制
- Sphinx
- Redis
- MongoDB
- ElasticSearch
- 接收用戶數據(Getting Data from Users)
- 創建表單(Creating Forms)
- 輸入驗證(Validating Input)
- 文件上傳(Uploading Files)
- 收集列表輸入(Collecting Tabular Input)
- 多模型同時輸入(Getting Data for Multiple Models)
- 顯示數據(Displaying Data)
- 格式化輸出數據(Data Formatting)
- 分頁(Pagination)
- 排序(Sorting)
- 數據提供器(Data Providers)
- 數據小部件(Data Widgets)
- 操作客戶端腳本(Working with Client Scripts)
- 主題(Theming)
- 安全(Security)
- 認證(Authentication)
- 授權(Authorization)
- 處理密碼(Working with Passwords)
- 客戶端認證(Auth Clients)
- 安全領域的最佳實踐(Best Practices)
- 緩存(Caching)
- 概述(Overview)
- 數據緩存(Data Caching)
- 片段緩存(Fragment Caching)
- 分頁緩存(Page Caching)
- HTTP 緩存(HTTP Caching)
- RESTful Web 服務
- 快速入門(Quick Start)
- 資源(Resources)
- 控制器(Controllers)
- 路由(Routing)
- 格式化響應(Response Formatting)
- 授權驗證(Authentication)
- 速率限制(Rate Limiting)
- 版本化(Versioning)
- 錯誤處理(Error Handling)
- 開發工具(Development Tools)
- 調試工具欄和調試器(Debug Toolbar and Debugger)
- 使用 Gii 生成代碼(Generating Code using Gii)
- TBD 生成 API 文檔(Generating API Documentation)
- 測試(Testing)
- 概述(Overview)
- 搭建測試環境(Testing environment setup)
- 單元測試(Unit Tests)
- 功能測試(Functional Tests)
- 驗收測試(Acceptance Tests)
- 測試夾具(Fixtures)
- 高級專題(Special Topics)
- 高級應用模版(Advanced Project Template)
- 從頭構建自定義模版(Building Application from Scratch)
- 控制臺命令(Console Commands)
- 核心驗證器(Core Validators)
- 國際化(Internationalization)
- 收發郵件(Mailing)
- 性能優化(Performance Tuning)
- 共享主機環境(Shared Hosting Environment)
- 模板引擎(Template Engines)
- 集成第三方代碼(Working with Third-Party Code)
- 小部件(Widgets)
- Bootstrap 小部件(Bootstrap Widgets)
- jQuery UI 小部件(jQuery UI Widgets)
- 助手類(Helpers)
- 助手一覽(Overview)
- Array 助手(ArrayHelper)
- Html 助手(Html)
- Url 助手(Url)