标签:8.1 v6.0 版本控制 name arr apiVersion uri version php
一,配置api多版本:
1,目录结构: 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://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,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返回: 2,访问:
http://192.168.219.6:8000/v2/home/tabbar返回:
四,查看php和thinkphp的版本:
php:liuhongdi@lhdpc:/data/php/admapi$ php --version PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.1, Copyright (c) Zend Technologies with Zend OPcache v8.1.1, Copyright (c), by Zend Technologiesthinkphp:
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/ liuhongdi@lhdpc:/data/php/admapi$ php think version v6.0.10LTS
标签:8.1,v6.0,版本控制,name,arr,apiVersion,uri,version,php 来源: https://www.cnblogs.com/architectforest/p/15851327.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。