功能概述
Request 类用于处理 HTTP 请求,提供获取 GET/POST 参数、请求头、客户端 IP 等功能,同时支持请求类型判断、类型转换、参数过滤与点路径取值。
两种调用方式
Request 提供「静态调用」与「实例调用」两种入口,二者读取的数据源不同,请按场景选择:
| 调用方式 | 数据来源 | 适用场景 |
|---|---|---|
Request::get('id')静态调用 | 调用时基于当前超全局变量构造的临时实例 | 快速取值;兼容旧代码 |
$request->get('id')实例调用 | 构造时捕获的请求快照(框架贯穿本次请求的唯一实例) | 推荐;可测试注入、可携带中间件附加数据 |
全局助手
input() 内部走的是实例调用(容器中绑定的当前请求),因此中间件通过 $request->foo = 'bar' 附加的数据同样可被读取。获取请求参数
GET 参数获取
// 获取单个 GET 参数
$id = Request::get('id');
// 指定默认值(键不存在时返回)
$page = Request::get('page', ['default' => 1]);
// 转换类型
$id = Request::get('id', ['type' => 'int']);
$price = Request::get('price', ['type' => 'float']);
// 应用过滤函数
$keyword = Request::get('keyword', ['function' => 'trim']);
// 组合使用多个选项
$id = Request::get('id', [
'default' => 0,
'type' => 'int',
'filter' => true,
]);
// 键名留空:返回全部 GET 数据
$query = Request::get();
POST 参数获取
// 获取单个 POST 参数
$username = Request::post('username');
// 获取所有 POST 数据
$allPostData = Request::post();
// 转换为数组
$tags = Request::post('tags', ['type' => 'array']);
// 简写:第二个参数传标量时视为默认值
$age = Request::post('age', 0);
// 应用多个处理函数(先 trim 再 strtolower)
$email = Request::post('email', [
'function' => ['trim', 'strtolower'],
'type' => 'string',
]);
获取任意来源输入
input() 合并 GET 与 POST,POST 优先,表单场景最为常用。
// 合并取值:同名键时取 POST 的值
$keyword = Request::input('keyword');
// 带默认值
$page = Request::input('page', 1);
// 带类型转换(第三个参数为处理选项)
$page = Request::input('page', 1, ['type' => 'int']);
// 不传键名:返回全部输入
$all = Request::input();
// 获取全部输入(等价写法)
$all = Request::all();
点路径取值 新增
键名支持以 . 分隔的点路径,可直接读取嵌套数组,无需层层判空:
// 表单字段 user[name] → $_POST['user']['name']
$name = Request::input('user.name');
// 数组下标同样支持
$firstId = Request::input('list.0.id');
// 与类型转换、默认值组合使用
$age = Request::input('user.profile.age', 0, ['type' => 'int']);
// 路径中任一层不存在时,安全回退默认值(不会触发未定义索引告警)
$phone = Request::input('user.contact.phone', '未填写');
// GET / POST 同样支持
$sort = Request::get('filter.sort', 'id');
点路径解析规则:先取顶层键,再逐层下钻;任一层不存在或不是数组,立即返回默认值。键名不含
. 时走快速路径,不产生额外拆分开销。类型转换 增强
通过处理选项 type 指定目标类型,转换发生在过滤函数之前:
| type 取值 | 转换结果 | 示例 |
|---|---|---|
string | 转为字符串 | '42' |
int | 转为整数 | 42 |
float | 转为浮点数 | 3.14 |
array | 转为数组 | ['a','b'] |
bool | 宽松布尔(见下) | false |
bool 采用宽松判定,以适配表单与查询串中的「字符串型布尔」:
// 以下写法均判为 false
// '0'、'false'、'off'、'no'、'null'、''
Request::post('agree', ['type' => 'bool']); // 'false' → false
// 以下写法均判为 true
// '1'、'true'、'on'、'yes'
Request::post('enable', ['type' => 'bool']); // 'on' → true
不要直接用 PHP 原生
(bool) 转换:(bool)'false' 的结果是 true,会把「关闭」误判为「开启」。框架的 bool 类型已处理该问题。处理选项一览
| 选项 | 说明 |
|---|---|
default | 键不存在或值为 null 时返回的默认值 |
type | 类型转换:string / int / float / array / bool |
function | 要应用的函数,可为函数名或函数名数组;支持 函数名:参数 形式 |
filter | 是否对字符串做 HTML 转义,默认 false(输入端不转义) |
原始数据获取
// 获取原始 POST 数据(未解析)
$rawData = Request::postInput();
// 获取并解析 JSON 请求体为关联数组
$jsonData = Request::getJson();
// 获取 JSON 请求体为对象
$jsonObject = Request::getJson(false);
实例化使用
$request = new Request();
// 获取所有请求参数(GET + POST 合并)
$allInputs = $request->all();
// 获取指定键名的输入,不存在则返回默认值
$name = $request->input('name', '游客');
// 点路径取值
$city = $request->input('user.address.city', '未知');
// 获取请求头
$contentType = $request->header('Content-Type', 'text/html');
// 获取当前请求实例(容器绑定,推荐在需要传递 Request 时使用)
$current = Request::current();
请求类型检查
// 获取请求方法,返回大写字符串
$method = Request::method(); // 'GET' / 'POST' / 'PUT' / 'DELETE' ...
// 判断请求方法
if (Request::isGet()) {
// GET 请求处理逻辑
}
if (Request::isPost()) {
// POST 请求处理逻辑
}
// 判断 AJAX 请求
$request = new Request();
if ($request->isAjax()) {
echo json_encode(['success' => true]);
}
// 判断 HTTPS 请求
if (Request::isHttps()) {
// 安全连接处理
}
POST 表单可通过
_method 字段伪装为 PUT / DELETE / PATCH,便于在纯 HTML 表单中实现 RESTful 语义。为避免绕过 CSRF 校验,不允许伪装为 GET 等安全方法。获取请求信息
// 获取所有请求头
$headers = Request::headers();
// 获取单个请求头(键名不区分大小写)
$userAgent = Request::header('User-Agent');
$acceptLanguage = Request::header('accept-language');
// 获取客户端 IP
$clientIp = Request::ip();
// 获取解析后的路由路径(已剥离查询串、入口文件与 URL 后缀)
$path = Request::path();
// 读取当前路由上下文(module / controller / action)
$module = Request::currentRoute('module');
客户端 IP 仅在
REMOTE_ADDR 命中可信代理列表(config: trusted_proxies)时才解析 X-Forwarded-For,且从右向左取第一个非可信代理地址,防止伪造请求头绕过登录日志、限流与审计。全局助手 input()
在控制器或视图中,可使用全局助手 input() 一步完成「取值 + 类型转换 + 默认兜底」:
input('id'); // 取 id(字符串)
input('id', 0, 'int'); // 转 int,缺省 0
input('user.name'); // 点路径取嵌套值
input('list.0.id', 0, 'int'); // 点路径下钻 + 类型转换
input('agree', false, 'bool'); // 宽松布尔
input('title', '', 'string', true); // 取值并转义
input(); // 返回全部输入
签名:input($key = null, $default = null, $type = '', $filter = false)。详见「助手函数(内置)」章节。
高级用法:组合参数处理
// 应用多个处理函数,并限制长度
// 使用 函数名:参数 格式,参数中的空白将替换为当前值
$title = Request::get('title', [
'function' => [
'trim',
'strip_tags',
'substr:,0,100',
],
]);
// 类型转换 + 自定义格式化函数
$amount = Request::post('amount', [
'type' => 'float',
'default' => 0.00,
'function' => 'number_format:,2',
]);
// 点路径 + 类型转换 + 默认值:一次调用完成列表页参数解析
$page = input('page', 1, 'int');
$perPage = input('per_page', 20, 'int');
$keyword = input('filter.keyword', '');
注意事项
filter选项默认为false——输入端保持原始数据,HTML 转义统一在输出层用e()完成,避免「输入转义 + 输出转义」造成的双重转义与数据污染。- 类型转换发生在过滤函数应用之前。
get()、post()、input()均通过Http::handling()处理参数,支持相同的处理选项。- 点路径中的任一层不存在时返回默认值,不会触发
Undefined array key告警。 - 静态调用(
Request::get())每次都会基于当前超全局变量构造临时实例;若需读取携带中间件附加数据的请求快照,请使用实例调用或Request::current()。