纵有疾风起
人生不言弃

thinkphp5.0 获取请求信息

如果要获取当前的请求信息,可以使用\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_STRINGecho '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.comfile: /index.phpurl: /index/index/hello.html?name=thinkphpurl with domain: http://tp5.com/index/index/hello.html?name=thinkphpurl without query: /index/index/hello.htmlroot:root with domain: http://tp5.compathinfo: index/index/hello.htmlpathinfo: index/index/helloext: html

 

设置/获取 模块/控制器/操作名称

$request = Request::instance();echo "当前模块名称是" . $request->module();echo "当前控制器名称是" . $request->controller();echo "当前操作名称是" . $request->action();

输出结果为:

当前模块名称是index当前控制器名称是HelloWorld当前操作名称是index

 

获取请求参数

$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)  请求参数:仅包含namearray (size=1)  'name' => string 'thinkphp' (length=8)  请求参数:排除namearray (size=1)  'test' => string 'ddd' (length=3)

获取路由和调度信息

$request = Request::instance();echo '路由信息:';dump($request->route());echo '调度信息:';dump($request->dispatch());

输出信息为:

路由信息: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');

  

 

  

文章转载于:https://www.cnblogs.com/shenzikun1314/p/7053217.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » thinkphp5.0 获取请求信息
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录