請(qǐng)求信息
如果要獲取當(dāng)前的請(qǐng)求信息,可以使用\think\Request
類,
除了下文中的
$request = Request::instance();
也可以使用助手函數(shù)
$request = request();
當(dāng)然,最方便的還是使用注入請(qǐng)求對(duì)象的方式來(lái)獲取變量。
例如:
獲取URL信息
$request = Request::instance();
// 獲取當(dāng)前域名
echo 'domain: ' . $request->domain() . '<br/>';
// 獲取當(dāng)前入口文件
echo 'file: ' . $request->baseFile() . '<br/>';
// 獲取當(dāng)前URL地址 不含域名
echo 'url: ' . $request->url() . '<br/>';
// 獲取包含域名的完整URL地址
echo 'url with domain: ' . $request->url(true) . '<br/>';
// 獲取當(dāng)前URL地址 不含QUERY_STRING
echo 'url without query: ' . $request->baseUrl() . '<br/>';
// 獲取URL訪問(wèn)的ROOT地址
echo 'root:' . $request->root() . '<br/>';
// 獲取URL訪問(wèn)的ROOT地址
echo 'root with domain: ' . $request->root(true) . '<br/>';
// 獲取URL地址中的PATH_INFO信息
echo 'pathinfo: ' . $request->pathinfo() . '<br/>';
// 獲取URL地址中的PATH_INFO信息 不含后綴
echo 'pathinfo: ' . $request->path() . '<br/>';
// 獲取URL地址中的后綴信息
echo 'ext: ' . $request->ext() . '<br/>';
輸出結(jié)果為:
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
設(shè)置/獲取 模塊/控制器/操作名稱
$request = Request::instance();
echo "當(dāng)前模塊名稱是" . $request->module();
echo "當(dāng)前控制器名稱是" . $request->controller();
echo "當(dāng)前操作名稱是" . $request->action();
如果當(dāng)前訪問(wèn)的地址是 http://serverName/index.php/index/hello_world/index
輸出結(jié)果為:
當(dāng)前模塊名稱是index
當(dāng)前控制器名稱是HelloWorld
當(dāng)前操作名稱是index
設(shè)置模塊名稱值需要向module方法中傳入名稱即可,同樣使用于設(shè)置控制器名稱和操作名稱
Request::instance()->module('module_name');
獲取請(qǐng)求參數(shù)
$request = Request::instance();
echo '請(qǐng)求方法:' . $request->method() . '<br/>';
echo '資源類型:' . $request->type() . '<br/>';
echo '訪問(wèn)ip地址:' . $request->ip() . '<br/>';
echo '是否AJax請(qǐng)求:' . var_export($request->isAjax(), true) . '<br/>';
echo '請(qǐng)求參數(shù):';
dump($request->param());
echo '請(qǐng)求參數(shù):僅包含name';
dump($request->only(['name']));
echo '請(qǐng)求參數(shù):排除name';
dump($request->except(['name']));
輸出結(jié)果為:
請(qǐng)求方法:GET
資源類型:html
訪問(wèn)ip地址:127.0.0.1
是否Ajax請(qǐng)求:false
請(qǐng)求參數(shù):
array (size=2)
'test' => string 'ddd' (length=3)
'name' => string 'thinkphp' (length=8)
請(qǐng)求參數(shù):僅包含name
array (size=1)
'name' => string 'thinkphp' (length=8)
請(qǐng)求參數(shù):排除name
array (size=1)
'test' => string 'ddd' (length=3)
獲取路由和調(diào)度信息
hello方法修改如下:
$request = Request::instance();
echo '路由信息:';
dump($request->route());
echo '調(diào)度信息:';
dump($request->dispatch());
路由定義為:
return [
'hello/:name' =>['index/hello',[],['name'=>'\w+']],
];
訪問(wèn)下面的URL地址:
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
調(diào)度信息:
array (size=2)
'type' => string 'module' (length=6)
'module' =>
array (size=3)
0 => null
1 => string 'index' (length=5)
2 => string 'hello' (length=5)
設(shè)置請(qǐng)求信息
如果某些環(huán)境下面獲取的請(qǐng)求信息有誤,可以手動(dòng)設(shè)置這些信息參數(shù),使用下面的方式:
$request = Request::instance();
$request->root('index.php');
$request->pathinfo('index/index/hello');
文檔最后更新時(shí)間:2018-04-26 09:16:25
輸入變量 →
未解決你的問(wèn)題?請(qǐng)到「問(wèn)答社區(qū)」反饋你遇到的問(wèn)題