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 ...
随机推荐
- 第三百七十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门搜索
第三百七十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门 我的搜素简单实现原理我们可以用js来实现,首先用js获取到 ...
- Java如何显示所有正在运行的线程?
在Java编程中,如何显示所有正在运行的线程? 以下示例演示如何使用getName()方法显示所有正在运行的线程的名称. package com.yiibai; public class Displa ...
- 前端图片压缩(纯js)
html代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- 简单介绍Linux下安装Tomcat的步骤
Tomcat是一个免费的开源的Serlvet容器,它是Apache基金会的Jakarta项目中的一个核心项目,由Apache,Sun和其它一些公司及个人共同开发而成.由于有了Sun的参与和支持,最新的 ...
- 谈谈Android中的Rect类——奇葩的思维
最近在工作中遇到了一些问题,总结下来就是Android中Rect这个类造成的.不得不说,不知道Android SDK的开发人员是怎么想的, 这个类设计的太奇葩了.首先介绍一下Rect类:Rect类主要 ...
- 【python】logging
https://docs.python.org/3/howto/logging.html import logging # create logger logger = logging.getLogg ...
- perforce变量配置与使用
linux Perforce 使用 1.配置环境变量: 下载 perforce 放到/usr/bin/下 export P4PORT=192.168.4.88:1666 #P4所在的主机exp ...
- ajax传JSON时设置的contenttype导致JAVA中request.getParameter("")怎么也接收不到数据
ajax传JSON时设置的contenttype默认值是application/x-www-form-urlencoded, 当ajax传JSON时设置的contenttype 如果是applicat ...
- scala akka Future 顺序执行 sequential execution
对于 A => B => C 这种 future 之间的操作,akka 默认会自动的按照顺序执行,但对于数据库操作来说,我们希望几个操作顺序执行,就需要使用语法来声明 有两种声明 futu ...
- 九度 1464:Hello World for U
题目描述: Given any string of N (>=5) characters, you are asked to form the characters into the shape ...