如果要獲取當前的請求信息,可以使用`\think\Request`類,
除了下文中的
~~~
$request = Request::instance();
~~~
也可以使用助手函數
~~~
$request = request();
~~~
> 當然,最方便的還是使用注入請求對象的方式來獲取變量。
例如:
## 獲取URL信息
~~~
$request = Request::instance();
// 獲取當前域名
echo 'domain: ' . $request->domain() . '<br/>';
// 獲取當前入口文件
echo 'file: ' . $request->baseFile() . '<br/>';
// 獲取當前URL地址 不含域名
echo 'url: ' . $request->url() . '<br/>';
// 獲取包含域名的完整URL地址
echo 'url with domain: ' . $request->url(true) . '<br/>';
// 獲取當前URL地址 不含QUERY_STRING
echo 'url without query: ' . $request->baseUrl() . '<br/>';
// 獲取URL訪問的ROOT地址
echo 'root:' . $request->root() . '<br/>';
// 獲取URL訪問的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/>';
~~~
輸出結果為:
~~~
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 "當前模塊名稱是" . $request->module();
echo "當前控制器名稱是" . $request->controller();
echo "當前操作名稱是" . $request->action();
~~~
如果當前訪問的地址是?[http://serverName/index.php/index/hello_world/index](http://servername/index.php/index/hello_world/index)?輸出結果為:
~~~
當前模塊名稱是index
當前控制器名稱是HelloWorld
當前操作名稱是index
~~~
設置模塊名稱值需要像module方法中傳入名稱即可,同樣使用于設置控制器名稱和操作名稱
~~~
Request::instance()->module('module_name');
~~~
## 獲取請求參數
~~~
$request = Request::instance();
echo '請求方法:' . $request->method() . '<br/>';
echo '資源類型:' . $request->type() . '<br/>';
echo '訪問地址:' . $request->ip() . '<br/>';
echo '是否AJax請求:' . var_export($request->isAjax(), true) . '<br/>';
echo '請求參數:';
dump($request->param());
echo '請求參數:僅包含name';
dump($request->only(['name']));
echo '請求參數:排除name';
dump($request->except(['name']));
~~~
輸出結果為:
~~~
請求方法:GET
資源類型:html
訪問地址: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方法修改如下:
~~~
$request = Request::instance();
echo '路由信息:';
dump($request->route());
echo '調度信息:';
dump($request->dispatch());
~~~
路由定義為:
~~~
return [
'hello/:name' =>['index/hello',[],['name'=>'\w+']],
];
~~~
訪問下面的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
調度信息:
array (size=2)
'type' => string 'module' (length=6)
'module' =>
array (size=3)
0 => null
1 => string 'index' (length=5)
2 => string 'hello' (length=5)
~~~
## 設置請求信息
如果某些環境下面獲取的請求信息有誤,可以手動設置這些信息參數,使用下面的方式:
~~~
$request = Request::instance();
$request->root('index.php');
$request->pathinfo('index/index/hello');
~~~
> 這節內容很重要,我們需要根據請求的信息(主要是URL方面)來處理不同業務,做出響應。