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_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 获取请求信息的更多相关文章
- Thinkphp5.0 的请求方式
Thinkphp5.0 的请求方式 方法一(使用框架提供的助手函数): public function index(){ $request = request(); dump($request); } ...
- Asp.Net Core获取请求信息/获取请求地址
一.Asp.Net Core 2.0版本中目前HttpRequest是个抽象类 在控制器或视图上下文中获取到的 Request对象,是 DefaultHttpRequest的实例. 定义 如图 : ...
- Flask_获取请求信息(三)
引用request的方法: from flask import request 与Django不同的是,flask是不需要将request对象作为第一个参数传入视图函数,他的request对象是来自于 ...
- 在dwr的调用类里获取请求信息
在dwr的调用类里获取请求的相关信息HttpSession session = WebContextFactory.get().getSession();HttpServletResponse res ...
- TP6.0 获取请求对象的五种方式
目录 1. 门面类 2. 依赖注入 3. 框架提供的基础控制器的 request 属性 4. request() 助手函数 5. app() 超级助手函数 think\Request.think\fa ...
- 常用Request对象获取请求信息
Request.ServerVariables(“REMOTE_ADDR”) ‘获取访问IPRequest.ServerVariables(“LOCAL_ADDR”) ‘同上Request.Serve ...
- Python+selenium之获取请求信息
basicConfig()所捕获的log信息.不过其开启的debug模式只能捕获到客户端像服务器发送的post()请求,而无法获取服务器所返回的应答信息. from random import ran ...
- thinkPHP5.0获取器获取原始数据
如果你定义了获取器的情况下,希望获取数据表中的原始数据,可以使用:$cate = Cate::get(1);// 通过获取器获取字段echo $cate->type;// 获取原始字段数据ech ...
- thinkPHP5.0获取器
获取器的作用是在获取数据的字段值后自动进行处理,例如,我们需要对状态值进行转换,可以使用: class Cate extends Model { public function getTypeAttr ...
随机推荐
- MCMC采样理论的一点知识
看了好多相关的知识,大致了解了一下马尔可夫链-蒙特卡罗采样理论,有必要记来下来. 蒙特卡罗积分:(来自:http://blog.csdn.net/itplus/article/details/1916 ...
- e833. 获得JTabbedPane中的卡片
This example retrieves all the tabs in a tabbed pane: // To create a tabbed pane, see e828 创建JTabbed ...
- Blog
http://www.cnblogs.com/digdeep/archive/2015/11/16/4968453.htmlhttp://it.dataguru.cn/article-8406-1.h ...
- mybatis plus 联合查询
在xml中只需要需要写如下的代码即可实现分页: <select id="selectUserList" parameterType="map" resul ...
- ASM - ByteCode插件下载、安装以及相关遇到的问题
http://asm.ow2.org/eclipse/index.html http://download.forge.objectweb.org/eclipse-update/ http://for ...
- 精选10款超酷的HTML5/CSS3菜单
今天向大家精选了10款超酷的HTML5/CSS3菜单,给你的网页添加不一样的精彩,一起来围观一下吧. 1.CSS3手风琴菜单 下拉展开带弹性动画 利用CSS3技术可以实现各种各样的网页菜单,我们之前也 ...
- Peckham添加引用文件模糊匹配智能提示
下载地址:https://github.com/markohlebar/Peckham 跟VVDocumenter规范注释生成器的安装方式一样: 下载开源工程在Xcode重新编译运行会自动安装此插 ...
- 使用 SharpSvn 执行 svn 操作的Demo
1. SharpSvn简介 SharpSvn.dll 是为.Net 2.0-4.0+ 应用提供的 Subversion Client API,更多详细介绍请见 https://sharpsvn.ope ...
- Android开发中常用的库总结(持续更新)
这篇文章用来收集Android开发中常用的库,都是实际使用过的.持续更新... 1.消息提示的小红点 微信,微博消息提示的小红点. 开源库地址:https://github.com/stefanjau ...
- js将json数据以csv格式下载
摘要: 最近有一个非项目的小需求,就是将项目开发分工文件化,方便后期管理维护.但是开发时,分工安排都是以json格式记录的,所以就做了一个将json数据以csv格式下载到本地. 代码: <!DO ...