tp5—请求信息

本文介绍了在ThinkPHP5框架中如何获取和设置请求信息,包括URL、模块、控制器、操作名称、请求参数、路由和调度信息,以及如何手动调整请求参数。
摘要由CSDN通过智能技术生成

如果要获取当前的请求信息,可以使用\think\Request类,
除了下文中的
$request = Request::instance();
也可以使用助手函数
$request = request();
当然,最方便的还是使用注入请求对象的方式来获取变量。
例如:

获取URL信息

$request = Request::instance();
// 获取当前域名
echo 'domain: ’ . $request->domain() . ‘
’;
// 获取当前入口文件
echo 'file: ’ . $request->baseFile() . ‘
’;
// 获取当前URL地址 不含域名
echo 'url: ’ . $request->url() . ‘
’;
// 获取包含域名的完整URL地址
echo 'url with domain: ’ . $request->url(true) . ‘
’;
// 获取当前URL地址 不含QUERY_STRING
echo 'url without query: ’ . $request->baseUrl() . ‘
’;
// 获取URL访问的ROOT地址
echo ‘root:’ . $request->root() . ‘
’;
// 获取URL访问的ROOT地址
echo 'root with domain: ’ . $request->root(true) . ‘
’;
// 获取URL地址中的PATH_INFO信息
echo 'pathinfo: ’ . $request->pathinfo() . ‘
’;
// 获取URL地址中的PATH_INFO信息 不含后缀
echo 'pathinfo: ’ . $request->path() . ‘
’;
// 获取URL地址中的后缀信息
echo 'ext: ’ . $request->ext() . ‘
’;
输出结果为:
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();
如果当前访问的地址是 http://serverName/index.php/index/hello_world/index
输出结果为:
当前模块名称是index
当前控制器名称是HelloWorld
当前操作名称是index
设置模块名称值需要向module方法中传入名称即可,同样使用于设置控制器名称和操作名称
Request::instance()->module(‘module_name’);

获取请求参数

$request = Request::instance();
echo ‘请求方法:’ . $request->method() . ‘
’;
echo ‘资源类型:’ . $request->type() . ‘
’;
echo ‘访问ip地址:’ . r e q u e s t − &gt; i p ( ) . ′ &lt; b r / &gt; ′ ; e c h o ′ 是 否 A J a x 请 求 : ′ . v a r e x p o r t ( request-&gt;ip() . &#x27;&lt;br/&gt;&#x27;; echo &#x27;是否AJax请求:&#x27; . var_export( request>ip().<br/>;echoAJax.varexport(request->isAjax(), true) . ‘
’;
echo ‘请求参数:’;
dump( r e q u e s t − &gt; p a r a m ( ) ) ; e c h o ′ 请 求 参 数 : 仅 包 含 n a m e ′ ; d u m p ( request-&gt;param()); echo &#x27;请求参数:仅包含name&#x27;; dump( request>param());echoname;dump(request->only([‘name’]));
echo ‘请求参数:排除name’;
dump($request->except([‘name’]));
输出结果为:
请求方法:GET
资源类型:html
访问ip地址:127.0.0.1
是否Ajax请求:false
请求参数:
array (size=2)
‘test’ => string ‘ddd’ (length=3)
‘name’ => string ‘thinkphp’ (length=8)

请求参数:仅包含name
array (size=1)
‘name’ => string ‘thinkphp’ (length=8)

请求参数:排除name
array (size=1)
‘test’ => string ‘ddd’ (length=3)

获取路由和调度信息

hello方法修改如下:
r e q u e s t = R e q u e s t : : i n s t a n c e ( ) ; e c h o ′ 路 由 信 息 : ′ ; d u m p ( request = Request::instance(); echo &#x27;路由信息:&#x27;; dump( request=Request::instance();echo;dump(request->route());
echo ‘调度信息:’;
dump($request->dispatch());
路由定义为:
return [
‘hello/:name’ =>[‘index/hello’,[],[‘name’=>’\w+’]],
];
访问下面的URL地址:
http://serverName/hello/thinkphp
输出信息为:
路由信息:
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’);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值