TP5中註冊路由後,在後臺無法獲取傳遞變數之解決
阿新 • • 發佈:2018-12-27
因為專案需要,隱藏url中的敏感路徑,就使用到了Tp5中的專案下route.php,但是按著手冊修改route.php後在後臺控制器中獲取不到引數值:
1、前提在config.php中新增:
'url_route_on' => true, //開啟路由
2、然後修改專案下 例如application/route.php
Route::bind('index'); //繫結模組
//批量註冊變數
Route::pattern([
'name' => '\w+',
'id' => '\d+',
'p' => '\d+'
]);
//路由規則
Route::get([
'jiameng/:name'=>['index/listShow',[],['name'=>'\w+']],
]);
public function listShow()
{
print_r($_GET); //獲取不到資料
dump(request()); //資料如下
}
dump(request()) 資料:
object(think\Request)#2 (32) { ["method":protected] => string(3) "GET" ["domain":protected] => NULL ["url":protected] => string(16) "/jiameng/feiyong" ["baseUrl":protected] => NULL ["baseFile":protected] => string(10) "/index.php" ["root":protected] => string(0) "" ["pathinfo":protected] => string(15) "jiameng/feiyong" ["path":protected] => string(15) "jiameng/feiyong" ["routeInfo":protected] => array(4) { ["rule"] => array(2) { [0] => string(7) "jiameng" [1] => string(5) ":name" } ["route"] => string(14) "index/listShow" ["option"] => array(1) { ["complete_match"] => bool(true) } ["var"] => array(1) { ["name"] => string(7) "feiyong" } } ["dispatch":protected] => array(3) { ["type"] => string(6) "module" ["module"] => array(3) { [0] => NULL [1] => string(5) "index" [2] => string(8) "listShow" } ["convert"] => bool(false) } ["module":protected] => string(5) "index" ["controller":protected] => string(5) "Index" ["action":protected] => string(8) "listShow" ["langset":protected] => string(5) "zh-cn" ["param":protected] => array(2) { ["/jiameng/feiyong"] => string(0) "" ["name"] => string(7) "feiyong" }["get":protected] => array(1) { ["/jiameng/feiyong"] => string(0) "" } ["post":protected] => array(0) { } ["request":protected] => array(0) { } ["route":protected] => array(1) { ["name"] => string(7) "feiyong" } ["put":protected] => NULL ["session":protected] => array(0) { } ["file":protected] => array(0) { } ["cookie":protected] => array(0) { } ["server":protected] => array(0) { } ["header":protected] => array(9) { ["accept-language"] => string(14) "zh-CN,zh;q=0.9" ["accept-encoding"] => string(13) "gzip, deflate" ["referer"] => string(27) "http://www.jm.com/index.php" ["accept"] => string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" ["user-agent"] => string(114) "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" ["upgrade-insecure-requests"] => string(1) "1" ["cache-control"] => string(9) "max-age=0" ["connection"] => string(5) "close" ["host"] => string(10) "www.jm.com" } ["mimeType":protected] => array(12) { ["xml"] => string(42) "application/xml,text/xml,application/x-xml" ["json"] => string(62) "application/json,text/x-json,application/jsonrequest,text/json" ["js"] => string(63) "text/javascript,application/javascript,application/x-javascript" ["css"] => string(8) "text/css" ["rss"] => string(19) "application/rss+xml" ["yaml"] => string(28) "application/x-yaml,text/yaml" ["atom"] => string(20) "application/atom+xml" ["pdf"] => string(15) "application/pdf" ["text"] => string(10) "text/plain" ["image"] => string(71) "image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*" ["csv"] => string(8) "text/csv" ["html"] => string(35) "text/html,application/xhtml+xml,*/*" } ["content":protected] => NULL ["filter":protected] => string(0) "" ["bind":protected] => array(0) { } ["input":protected] => string(0) "" ["cache":protected] => NULL ["isCheckCache":protected] => NULL }
說明name在request中的route中是存在的。
後來直接在控制中列印:
var_dump(request()->route('name')); //輸出 feiyong
4、檢視原始碼後發現可以直接在控制器下的方法中傳遞形參可以減少程式碼量 優化為:
public function listShow($name)
{
echo $name; //輸出為 feiyong
}
至此就可以愉快的玩耍route了