請求信息
如果要獲取當前的請求信息,可以使用\think\Request類,
除了下文中的
$request = Request::instance();
也可以(yi)使用助手(shou)函數
$request = request();
當(dang)然,最方便的(de)還是使用注入請求(qiu)對象的(de)方式來獲取變量。
例如:
獲取URL信息
$request = Request::instance();
// 獲取當前(qian)域名
echo 'domain: ' . $request->domain() . '<br/>';
// 獲取當前(qian)入口文件
echo 'file: ' . $request->baseFile() . '<br/>';
// 獲取當前URL地址 不(bu)含域名
echo 'url: ' . $request->url() . '<br/>';
// 獲(huo)取(qu)包含域名的完(wan)整URL地址
echo 'url with domain: ' . $request->url(true) . '<br/>';
// 獲(huo)取(qu)當前URL地(di)址 不含QUERY_STRING
echo 'url without query: ' . $request->baseUrl() . '<br/>';
// 獲(huo)取URL訪問(wen)的(de)ROOT地址
echo 'root:' . $request->root() . '<br/>';
// 獲(huo)取URL訪問的ROOT地(di)址(zhi)
echo 'root with domain: ' . $request->root(true) . '<br/>';
// 獲取URL地址中(zhong)的(de)PATH_INFO信(xin)息
echo 'pathinfo: ' . $request->pathinfo() . '<br/>';
// 獲取(qu)URL地(di)址中(zhong)的PATH_INFO信息 不(bu)含(han)后綴
echo 'pathinfo: ' . $request->path() . '<br/>';
// 獲取URL地(di)址中的(de)后綴信息
echo 'ext: ' . $request->ext() . '<br/>';
輸出結果為:
domain: http://tp5.com
file: /index.php
url: /index/index/hello.html?name=thinkphp
url with domain: http://tp5.com/index/index/hello.html?name=thinkphp
url without query: /index/index/hello.html
root:
root with domain: http://tp5.com
pathinfo: index/index/hello.html
pathinfo: index/index/hello
ext: html
設置/獲取 模塊/控制器/操作名稱
$request = Request::instance();
echo "當前模塊(kuai)名稱是" . $request->module();
echo "當前控(kong)制器(qi)名稱是" . $request->controller();
echo "當前操作名稱是" . $request->action();
如果當前訪問的地址是
輸出結果為:
當前模塊名稱是index
當前控制器名稱是HelloWorld
當前操作名稱是index
設置模塊名(ming)稱(cheng)值(zhi)需要向module方法(fa)中傳入名(ming)稱(cheng)即可,同(tong)樣使用于(yu)設置控(kong)制器名(ming)稱(cheng)和操作名(ming)稱(cheng)
Request::instance()->module('module_name');
獲取請求參數
$request = Request::instance();
echo '請求方法(fa):' . $request->method() . '<br/>';
echo '資源(yuan)類型:' . $request->type() . '<br/>';
echo '訪問ip地址:' . $request->ip() . '<br/>';
echo '是否(fou)AJax請求:' . var_export($request->isAjax(), true) . '<br/>';
echo '請求參(can)數:';
dump($request->param());
echo '請求參數:僅包含name';
dump($request->only(['name']));
echo '請求參數(shu):排(pai)除name';
dump($request->except(['name']));
輸出結果為:
請求方法:GET
資源類型:html
訪問ip地址:127.0.0.1
是否Ajax請求:false
請求參數:
array (size=2)
'test' => string 'ddd' (length=3)
'name' => string 'thinkphp' (length=8)
請求參數:僅包含name
array (size=1)
'name' => string 'thinkphp' (length=8)
請求參數:排除name
array (size=1)
'test' => string 'ddd' (length=3)
獲取路由和調度信息
hello方(fang)法(fa)修改如下:
$request = Request::instance();
echo '路由信息(xi):';
dump($request->route());
echo '調度信息(xi):';
dump($request->dispatch());
路由定義為:
return [
'hello/:name' =>['index/hello',[],['name'=>'\w+']],
];
訪問下面的URL地址(zhi):
http://serverName/hello/thinkphp
輸出信息為:
路由信息:
array (size=4)
'rule' => string 'hello/:name' (length=11)
'route' => string 'index/hello' (length=11)
'pattern' =>
array (size=1)
'name' => string '\w+' (length=3)
'option' =>
array (size=0)
empty
調度信息:
array (size=2)
'type' => string 'module' (length=6)
'module' =>
array (size=3)
0 => null
1 => string 'index' (length=5)
2 => string 'hello' (length=5)
設置請求信息
如果某些環境下(xia)面獲取的請(qing)求信息有誤,可(ke)以手動設置這些信息參數,使用下(xia)面的方(fang)式(shi):
$request = Request::instance();
$request->root('index.php');
$request->pathinfo('index/index/hello');
文檔最后更新時間:2018-04-26 09:16:25
輸入變量 →
未解決你的問題?請到「問答社區」反饋你遇到的問題
