如果要获取当前的请求信息,可以使用\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();

输出结果为:

当前模块名称是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=)
'test' => string 'ddd' (length=)
'name' => string 'thinkphp' (length=) 请求参数:仅包含name
array (size=)
'name' => string 'thinkphp' (length=) 请求参数:排除name
array (size=)
'test' => string 'ddd' (length=)

获取路由和调度信息

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

输出信息为:

路由信息:
array (size=)
'rule' => string 'hello/:name' (length=)
'route' => string 'index/hello' (length=)
'pattern' =>
array (size=)
'name' => string '\w+' (length=)
'option' =>
array (size=)
empty 调度信息:
array (size=)
'type' => string 'module' (length=)
'module' =>
array (size=)
=> null
=> string 'index' (length=)
=> string 'hello' (length=)

设置请求信息

如果某些环境下面获取的请求信息有误,可以手动设置这些信息参数,使用下面的方式:

$request = Request::instance();
$request->root('index.php');
$request->pathinfo('index/index/hello');

  

 

  

thinkphp5.0 获取请求信息的更多相关文章

  1. Thinkphp5.0 的请求方式

    Thinkphp5.0 的请求方式 方法一(使用框架提供的助手函数): public function index(){ $request = request(); dump($request); } ...

  2. Asp.Net Core获取请求信息/获取请求地址

     一.Asp.Net Core 2.0版本中目前HttpRequest是个抽象类 在控制器或视图上下文中获取到的 Request对象,是 DefaultHttpRequest的实例. 定义 如图 : ...

  3. Flask_获取请求信息(三)

    引用request的方法: from flask import request 与Django不同的是,flask是不需要将request对象作为第一个参数传入视图函数,他的request对象是来自于 ...

  4. 在dwr的调用类里获取请求信息

    在dwr的调用类里获取请求的相关信息HttpSession session = WebContextFactory.get().getSession();HttpServletResponse res ...

  5. TP6.0 获取请求对象的五种方式

    目录 1. 门面类 2. 依赖注入 3. 框架提供的基础控制器的 request 属性 4. request() 助手函数 5. app() 超级助手函数 think\Request.think\fa ...

  6. 常用Request对象获取请求信息

    Request.ServerVariables(“REMOTE_ADDR”) ‘获取访问IPRequest.ServerVariables(“LOCAL_ADDR”) ‘同上Request.Serve ...

  7. Python+selenium之获取请求信息

    basicConfig()所捕获的log信息.不过其开启的debug模式只能捕获到客户端像服务器发送的post()请求,而无法获取服务器所返回的应答信息. from random import ran ...

  8. thinkPHP5.0获取器获取原始数据

    如果你定义了获取器的情况下,希望获取数据表中的原始数据,可以使用:$cate = Cate::get(1);// 通过获取器获取字段echo $cate->type;// 获取原始字段数据ech ...

  9. thinkPHP5.0获取器

    获取器的作用是在获取数据的字段值后自动进行处理,例如,我们需要对状态值进行转换,可以使用: class Cate extends Model { public function getTypeAttr ...

随机推荐

  1. e830. 向JTabbedPane中加入一个卡片

    This example demonstrates various ways to add a tab to a tabbed pane. // Create a tabbed pane JTabbe ...

  2. Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

    严重:   Error   configuring   application   listener   of   class   org.springframework.web.context.Co ...

  3. iText操作PDF读取JPEG图片ArrayIndexOutOfBoundsException异常

    iText版本:itextpdf-5.5.1.jar 问题描述 读取本地JPEG图片文件的代码: com.itextpdf.text.Image image = com.itextpdf.text.I ...

  4. T4使用经验

    .<#@ template debug="true" hostspecific="true" language="C#" #> ...

  5. MarkDown 使用说明示例

    一.标题 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 一级标题 这是 H2 这是 H3 一级和二级标题还有一种写法 就是下面加横杆,同时 超过2个的 = 和 - 都可以有效果. Thi ...

  6. 浪漫程序员 HTML5爱心表白动画

    我们程序员在追求爱情方面也是非常浪漫的,下面是一位同学利用自己所学的HTML5知识自制的HTML5爱心表白动画,画面非常温馨甜蜜,这样的创意很容易打动女孩,如果你是单身的程序员,也赶紧来制作自己的爱心 ...

  7. sparkSQL实际应用

    提交代码包 /usr/local/spark/bin$ spark-submit --class "getkv" /data/chun/sparktes.jar 1.查询KV im ...

  8. Uploading File using Ajax and receiving binary data in Asp.net (C#)[转]

    基础知识,可由此衍生.原文:http://uniapple.net/blog/?p=2050 In this post, I will show you how to upload a file us ...

  9. 自动换行后缩进怎么做(CSS)?(可用于 Li y 元素的排版)

    <style type="text/css">li{ width:100px; border:1px solid #ccc; padding-left:25px; te ...

  10. 将nosetests的echo结果保存到本地文件

    nose是很好用的python 测试框架. 但是一直很纠结如何将结果保存到本地.采用nosetests -h查看相关的options,找到一个xunit的东西,似乎可以实现功能. 测试结果: 可见,已 ...