例:/?service=App/Site/Index
service如何獲取毫缆,可查看phalapi官方文檔
如何重寫(xiě)請(qǐng)求,在哪重寫(xiě)請(qǐng)求败富,可查看phalapi官方文檔
請(qǐng)求目錄結(jié)構(gòu)
請(qǐng)求目錄結(jié)構(gòu)
composer配置
composer配置
functions.php的使用:\App\V1\Hello();
版本為 1.0.0的請(qǐng)求方法 及 版本為 1.0.1請(qǐng)求到的方法
初版和二版
以下為具體代碼實(shí)現(xiàn);
// 優(yōu)先返回自定義格式的接口服務(wù)名稱(chēng)
$header = $this->getHeader("HTTP_VERSION");//自定義版本命名
//獲取更新版本
$versionArray = array_filter(explode(".",$header),function($v){if(is_null($v))return false;else{return true;}}); //array(3) {[0]=>string(1) "1",[1]=>string(1) "0",[2]=>string(1) "1"}
$version = "v".reset($versionArray); //v1
$version = strtoupper($version);//小寫(xiě)變大寫(xiě) V1
if(!$header || count($versionArray)<3){
return '版本丟失';
}
$serviceArray = explode("/",$service);//array(3) {[0]=>string(3) "App",[1]=>string(4) "Site",[2]=>string(5) "Index"}
if(count($serviceArray) == 2){
array_unshift($serviceArray,"App");
}
//判斷公共方法悔醋,取消版本驗(yàn)證 (自定義)
//if(in_array(reset($serviceArray),$this->noVersion())){
// return implode($serviceArray,".");
//}
//訪(fǎng)問(wèn)具體版本模塊 (后期可優(yōu)化為加密token)
$namespace = count($serviceArray) == 2 ? 'App_V1' : reset($serviceArray)."_".$version;
$apiName = $serviceArray[count($serviceArray)-2];
$end = end($serviceArray);
$actionVersion = $end;
//小版本迭代時(shí),需要訪(fǎng)問(wèn)的具體方法兽叮,(后期可優(yōu)化為加密token)
if(($versionArray[1]>0 || $versionArray[2]>0)){
$functionVisit = $versionArray[1].".".$versionArray[2];
//小版本向下兼容
$versionCompatibility= $this->actionDownwardCompatibility($namespace,$apiName,$end,$functionVisit);
if($versionCompatibility){
$actionVersion = $versionCompatibility;
}
}
$actionName = $actionVersion;
return $namespace .".".$apiName.".".$actionName;// 最終路由:App_V1.User_Info.Index_0_1
actionDownwardCompatibility 方法芬骄,查找文件中是否含有 類(lèi) 及 方法
/**
* 版本向下兼容
* @param $namespace string App_V1
* @param $api string Lists_Courses
* @param $action string Index
* @param $versionNum string 1.1
* @return bool
*/
private function actionDownwardCompatibility($namespace,$api,$action,$versionNum){
$namespace = str_replace("_",'\\',$namespace);
$className = '\\' . $namespace . '\\Api\\' . str_replace('_', '\\', ucfirst($api));
$rClass = new \ReflectionClass($className);
for ($i=floatval($versionNum);$i>=0.1;$i=$i-0.1){
$version = str_replace(".",'_',number_format($i,1));
$newAction = $action."_".$version;
if($rClass->hasMethod($newAction)){
return $newAction;
}
}
return false;
}