一,配置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://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/06/01/thinkphp6-api-duo-ban-ben-kong-zhi-php-8-1-1-thinkphp-v6/
對應的源碼可以訪問這里獲取: 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 Technologies
thinkphp:
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/ liuhongdi@lhdpc:/data/php/admapi$ php think version v6.0.10LTS