开发中间件遇报错:Trying to get property 'headers' of non-object 求助
Hey there! Let's work through this error you're facing. That Trying to get property 'headers' of non-object message is telling you that whatever you're trying to access the headers property on isn't actually a Request object—let's break down the most common fixes and checks you can do:
Common Causes & Solutions
1. The variable you're using isn't actually a Request instance
First off, let's confirm what you're working with. Right before the line that throws the error, add a debug check to see what the variable contains:
// Replace $yourRequestVar with the variable you're trying to access var_dump($yourRequestVar); die();
Chances are this will show null, an array, or a string instead of a Request object. This usually happens because:
- You're not receiving the Request correctly from your framework. For example, in PHP frameworks like Laravel, middleware methods should accept the Request as the first parameter:
public function handle(Request $request, Closure $next) { // Now $request is a valid object, so $request->headers will work return $next($request); } - There's a typo in your variable name (e.g.,
$reqestinstead of$request) leading to an undefined variable that defaults tonull.
2. Your middleware is running too early in the stack
Many frameworks execute middleware in a specific order. If your middleware runs before the framework has fully initialized the Request object, you'll end up trying to access properties on an uninitialized (non-object) value.
- Check your middleware registration order. Make sure your middleware comes after any core framework middleware that sets up the Request (like request parsing or bootstrapping middleware).
3. You're misunderstanding how to access the Request
You mentioned trying to "get the request from an input field"—this might be a key point of confusion. In most middleware setups, the Request object is provided to your middleware by the framework, not pulled from an input field. Input fields are data inside the Request (like form data or query params), but the Request itself is a framework-managed object.
- If you're trying to extract data from the Request's input, you'd use something like
$request->input('field_name')—but theheadersproperty is part of the Request object itself, not an input field.
4. Your framework uses a different method to access headers
Double-check your framework's documentation. Some frameworks don't expose headers directly as a property—they might use a method instead. For example:
- Instead of
$request->headers, you might need$request->getHeaders() - Or to get a specific header:
$request->header('Content-Type')
Next Steps
Start with that debug var_dump—it'll tell you exactly what you're working with, which is the fastest way to narrow down the problem. Once you know whether the variable is null, an array, or something else, you can adjust your code to properly get the valid Request object.
内容的提问来源于stack exchange,提问作者Дмитрий Смирнов

