Laravel Trying to get property 'headers' of non-object

F:\laragon\laragon\www\english\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php
        $token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN');
 
        if (! $token && $header = $request->header('X-XSRF-TOKEN')) {
            $token = $this->encrypter->decrypt($header, static::serialized());
        }
 
        return $token;
    }
 
    /**
     * Add the CSRF token to the response cookies.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Symfony\Component\HttpFoundation\Response  $response
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function addCookieToResponse($request, $response)
    {
        $config = config('session');
        $response->headers->setCookie(
            new Cookie(
                'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']),
                $config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null
            )
        );
 
        return $response;
    }
 
    /**
     * Determine if the cookie contents should be serialized.
     *
     * @return bool
     */
    public static function serialized()
    {
        return EncryptCookies::serialized('XSRF-TOKEN');
    }
}
 
Arguments
"Trying to get property 'headers' of non-object"

最近做项目老遇到这个问题,百度了N久也没找到What’s happened?每次遇到这个问题就解决不了,今天下了决心搞定这个问题,没想到真被我找到了问题的源头!!!

$response->headers->setCookie$response打印了是个null

于是就去找中间件,看到底是哪里出了问题,终于被我找到了,其中有个中间件这么写的

$accountInfo = Auth::user();
if (!$accountInfo) return null;

自己造的孽还要自己解决!在此处获取了缓存信息,信息过期了了就返回了null结果就不行了,修改之后,获取不到了就到登录页或返回错误页面,报错搞定

$accountInfo = Auth::user();
if (!$accountInfo)   return response()->view('errors.401', compact('previousUrl'));

你可能感兴趣的:(PHP)