TP6.0中Request对象不再自动注入控制器,需显式继承think\controller\Controller或手动构造注入;input()函数废弃,应改用$request->param()/post()/get()/file()等方法。

input()、$this->request->param() 报错,不是 Request 对象没加载,而是调用方式或依赖注入没对上——TP6.0 的 Request 实例不再自动绑定到控制器属性,必须显式获取或正确继承基类。
控制器里直接用 $this->request 报错 Class not found 或 Call to a member function param()
TP6.0 默认控制器不再自动注入 Request 对象,$this->request 属性在未声明或未继承正确基类时是 null。这不是 bug,是容器绑定策略收紧后的显式化要求。
- 确认控制器是否继承
think\Controller(TP6.0 中该类已移至think\controller\Controller),且顶部use think\controller\Controller;存在 - 若用自定义基类,需在构造函数中手动注入:
public function __construct(Request $request) { $this->request = $request; } - 不推荐在非控制器类中硬编码
$this->request;应通过依赖注入传入Request实例,或用app('request')获取(但注意:后者在命令行或中间件中可能不可用)
input('post.name') 迁移后取不到数据
TP6.0 废弃了全局 input() 函数,且请求变量不再自动合并(get/post/put/delete 分离更严格)。原写法 input('post.name') 在 TP6 中无对应语法,必须按方法类型显式调用。
-
input('name')→ 改为$this->request->param('name')(兼容所有类型,按 Content-Type 自动解析) -
input('post.name')→ 改为$this->request->post('name')(仅取 POST 数据) -
input('get.id')→ 改为$this->request->get('id')(GET 参数必须用get(),不能混用param()依赖路由参数) - 上传文件统一用
$this->request->file('avatar'),不再支持input('file.')写法
中间件或服务类里无法访问请求数据
中间件的 handle() 方法签名在 TP6 中强制要求 \Closure $next 类型提示,同时 Request 是第一个参数,不是 $this 属性——所以不能写 $this->request->xxx。
- 中间件中必须从方法参数获取:
public function handle(Request $request, \Closure $next) - 服务类、验证器、模型等非请求上下文类,不能依赖
$this->request;如需请求数据,应由调用方传入,或通过容器绑定(但不推荐,会增加耦合) - 若在模型中需要当前请求 ID 做日志追踪,建议用
app('request')->header('x-request-id', uniqid()),而非尝试注入
最常被忽略的一点:TP6.0 的 Request 对象默认不解析 JSON 请求体,除非请求头含 Content-Type: application/json 且配置中启用了 json_input。如果前端用 fetch 发 JSON 却收不到数据,先检查 config/app.php 是否有 'json_input' => true。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











