thinkphp6-请求
请求对象
构造方法注入
控制器 app/controller/Index.php
<?php
namespace app\controller;
use think\Request;
class Index
{
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function index()
{
return $this->request->param('name');
}
}
测试与结果
http://127.0.0.1:8000/index?name=huyongjian
huyongjian
操作方法注入
控制器 app/controller/Index.php
<?php
namespace app\controller;
use think\Request;
class Index
{
public function index(Request $request)
{
return $request->param('age');
}
}
测试与结果
http://127.0.0.1:8000/index?age=32
32
静态调用
控制器 app/controller/Index.php
<?php
namespace app\controller;
use think\facade\Request;
class Index
{
public function index()
{
return Request::param('username');
}
}
测试与结果
http://127.0.0.1:8000/index?username=huyongjian
huyongjian
助手函数
控制器 app/controller/Index.php
<?php
namespace app\controller;
class Index
{
public function index()
{
return request()->param('name');
}
}
测试与结果
http://127.0.0.1:8000/index?name=xiaoming
xiaoming
请求信息
请求信息
控制器
<?php
namespace app\controller;
use think\facade\Request;
class Index
{
public function index()
{
return Request::url();
}
}
测试与结果
http://127.0.0.1:8000/index?name=xiaoming
/index?name=xiaoming
支持获取的请求信息
方法 含义
host 当前访问域名或者IP
scheme 当前访问协议
port 当前访问的端口
remotePort 当前请求的REMOTE_PORT
protocol 当前请求的SERVER_PROTOCOL
contentType 当前请求的CONTENT_TYPE
domain 当前包含协议的域名
subDomain 当前访问的子域名
panDomain 当前访问的泛域名
rootDomain 当前访问的根域名
url 当前完整URL
baseUrl 当前URL(不含QUERY_STRING)
query 当前请求的QUERY_STRING参数
baseFile 当前执行的文件
root URL访问根地址
rootUrl URL访问根目录
pathinfo 当前请求URL的pathinfo信息(含URL后缀)
ext 当前URL的访问后缀
time 获取当前请求的时间
type 当前请求的资源类型
method 当前请求类型
rule 当前请求的路由对象实例
控制器/操作
控制器
Request::controller();
Request::controller(true);//小写
操作
Request::action();
Request::action(true);//小写
转小写
parse_name(Request::controller());
parse_name(Request::action());
内置方法
变量检测与获取
控制器 app/controller/Index.php
<?php
namespace app\controller;
use think\facade\Request;
class Index
{
public function index()
{
//检测变量
$bool = Request::has('id', 'get');
var_dump($bool);
//获取变量
// 获取当前请求的name变量
$name = Request::param('name','XiaoMing');
var_dump($name);
}
}
测试与结果
http://127.0.0.1:8000/index?id=2&name=huyongjian
bool(true) string(10) "huyongjian"
过滤
全局过滤
Request类 app/Request.php
<?php
namespace app;
// 应用请求对象类
class Request extends \think\Request
{
protected $filter = ['strip_tags'];
}
控制器 app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\Request;
class Index extends BaseController
{
public function index()
{
$str = Request::get('str');
var_dump($str);
}
}
测试与结果
http://127.0.0.1:8000/index?str=Hello%20%3Cb%3Eworld!%3C/b%3E
string(12) "Hello world!"
局部过滤
控制器 app/controller/Index.php
<?php
namespace app\controller;
use think\facade\Request;
class Index
{
public function index()
{
Request::filter(['strip_tags']);
$str = Request::get('str');
var_dump($str);
}
}
获取方法过滤
Request::get('name','','htmlspecialchars'); // 获取get变量 并用htmlspecialchars函数过滤
Request::param('username','','strip_tags'); // 获取param变量 并用strip_tags函数过滤
Request::post('name','','org\Filter::safeHtml'); // 获取post变量 并用org\Filter类的safeHtml方法过滤
判断请求类型
用途 方法
获取当前请求类型 method
判断是否GET请求 isGet
判断是否POST请求 isPost
判断是否PUT请求 isPut
判断是否DELETE请求 isDelete
判断是否AJAX请求 isAjax
判断是否PJAX请求 isPjax
判断是否JSON请求 isJson
判断是否手机访问 isMobile
判断是否HEAD请求 isHead
判断是否PATCH请求 isPatch
判断是否OPTIONS请求 isOptions
判断是否为CLI执行 isCli
判断是否为CGI模式 isCgi
请求头信息
$info = Request::header();
echo $info['accept'];
echo $info['accept-encoding'];
echo $info['user-agent'];
请求缓存
// 定义GET请求路由规则 并设置3600秒的缓存
Route::get('new/:id','News/read')->cache(3600);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通