thinkphp5获取URL信息

2019-04-04 10:27:17 6944

如果要获取当前的请求信息,可以使用\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: https://www.landui.com

file: /index.php

url: /index/index/hello.html?name=thinkphp

url with domain: https://www.landui.com/index/index/hello.html?name=thinkphp

url without query: /index/index/hello.html

root:root with domain: https://www.landui.com

pathinfo: index/index/hello.html

pathinfo: index/index/hello

ext: html


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: