Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Zewail edited this page Sep 11, 2018 · 1 revision

如果你使用了自定义路由,则可以使用路由版本管理

版本组

使用think-api的版本管理方法来创建版本

$api = new \Zewail\Api\Routing\Router;
$api->version('v1', function () {
	// TODO 可以是thinkphp自带的路由
});

或者使用门面(Facede)

use Zewail\Api\Facades\ApiRoute;
ApiRoute::version('v1', function(){
 // TODO 可以是thinkphp自带的路由
});

你想一个分组返回多个版本,只需要传递一个版本数组

ApiRoute::version(['v1', 'v2'], function () {
	// TODO 可以是thinkphp自带的路由
});
创建路由
ApiRoute::version('v1', function(){
 Route::rule('new/:id','index/News/read');
});

因为每个版本分组了,你可以为相同 URL 上的同一个路由创建不同响应

ApiRoute::version('v1', function () {
	Route::rule('new/:id','app\index\controller\V2\News@read');
 	// 或者
 	Route::rule('new/:id','index/V2.News/read');
});
ApiRoute::version('v2', function () {
 Route::rule('new/:id','app\index\controller\V2\News@read');
});
访问特定路由版本

默认访问配置文件中的默认版本

但是,我们可以在Http的头信息中附带Api-Version参数,或者直接在在url或body中附带version参数来访问指定版本

http://example.com/new/102?version=v2

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /