“如何修复'尝试获取非对象的属性'标题'”VerifyCsrfToken

I run my project and i get this error:

Trying to get property 'headers' of non-object in "\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php"

in this function

    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;
    }

What are you actually passing as $response argument in this case?

I believe one needs more details to come up with the concrete cause of why you are getting the error, however I can try to give a hint:

For some reason what you pass in place of $response is not being recognized as an instance of an object in your case. You could try instantiating it like so:

$response = Response::make($contents, $statusCode);

$response->header('Content-Type', $value);

and then pass it to your function.

Maybe you can try and trace back based on this assumption, where exactly in your Project the chain is broken?

I mean, in your case it is a parameter in your function, but how would it know that it inherits from the Symfony\Component\HttpFoundation\Response class?

Maybe you should "typehint" it -> like say Response $response in the brackets.

I found this link here to be useful explanation also. In the post they talk about Request and not Response, but I think the principle of the issue is is related:

https://www.quora.com/What-does-Request-request-mean-in-Laravel

You can check out the documentation:

https://laravel.com/docs/4.2/responses