一,配置api多版本:
1,目录结构:<?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 <[email protected]> // +---------------------------------------------------------------------- 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
说明:作者:刘宏缔 邮箱: [email protected]
二,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返回:
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。