简体   繁体   English

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

[英]“How to fix ‘Trying to get property 'headers' of non-object” 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" 试图在“\\ 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? 在这种情况下,你实际上作为$ response参数传递了什么?

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. 出于某种原因,您传递的代替$ response的内容未被识别为您案例中的对象实例。 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? 我的意思是,在你的情况下它是你函数中的一个参数,但它怎么知道它继承自Symfony \\ Component \\ HttpFoundation \\ Response类?

Maybe you should "typehint" it -> like say Response $response in the brackets. 也许你应该“键入”它 - >比如说括号中的Response $响应。

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 https://www.quora.com/What-does-Request-request-mean-in-Laravel

You can check out the documentation: 你可以查看文档:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM