多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 一,配置api多版本: 1,目录结构: ![](https://img.kancloud.cn/93/97/9397e1194140355ded6bb73212581918_321x478.png) 2,路由: route/app.php ~~~ <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- use think\facade\Route; $arr_version = ["v1","v2"]; $uri = request()->server('REQUEST_URI'); $arr_uri = explode("/",$uri); global $apiVersion; if (!isset($arr_uri[1]) || trim($arr_uri[1]) == '') { $apiVersion = ""; } else { $tmpVersion = trim($arr_uri[1]); if (in_array($tmpVersion,$arr_version)) { $apiVersion = $tmpVersion; } else { $apiVersion = ""; } } //Route::rule($version.'/:controller/:function', $version.'.:controller/:function'); Route::group($apiVersion.'/home',function() { global $apiVersion; Route::get('tabbar',$apiVersion.'.Home/tabbar') ; //Route::get('/recent','api/:version.Product/getRecent') ; }); ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ## 二,php代码开发: 1,controller/v1/Home.php ~~~ <?php declare (strict_types = 1); namespace app\controller\v1; use app\BaseController; use app\result\Result; class Home extends BaseController { public function tabBar() { $button = [ ['id'=>1,'name'=>'首页'], ['id'=>2,'name'=>'商品列表'], ['id'=>3,'name'=>'我的'], ]; return Result::Success(["button"=>$button]); } } ~~~ 2,controller/v2/Home.php ~~~ <?php declare (strict_types = 1); namespace app\controller\v2; use app\BaseController; use app\result\Result; class Home extends BaseController { public function tabBar() { $button = [ ['id'=>1,'name'=>'首页'], ['id'=>2,'name'=>'商品列表'], ['id'=>3,'name'=>'购物车'], ['id'=>4,'name'=>'我的'], ]; return Result::Success(["button"=>$button]); } } ~~~ ## 三,测试效果: 1,访问: ~~~ http://192.168.219.6:8000/v1/home/tabbar ~~~ 返回: ![](https://img.kancloud.cn/c6/b8/c6b8359e07f163a79e4d1731ab38637a_916x762.png) 2,访问: ~~~ http://192.168.219.6:8000/v2/home/tabbar ~~~ 返回: ![](https://img.kancloud.cn/d5/7b/d57b084807eddf098b1cc77f0abd518b_914x910.png)