Laravel 5.4 - Cookie 队列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45180963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:19:41  来源:igfitidea点击:

Laravel 5.4 - Cookie Queue

laravelcookiesdingo-api

提问by Christian

I'm using Laravel 5.4 and I wrote something like:

我正在使用 Laravel 5.4,我写了一些类似的东西:

     Cookie::queue(
        'refresh_token',
        $data->refresh_token,
        864000, // 10 days
        null,
        null,
        false,
        true // HttpOnly
    );

    return response('hello world');

The returned response doesn't contain the refresh_token cookie while return response('hello world')->withCookie(...)does.

返回的响应不包含 refresh_token cookie 而包含return response('hello world')->withCookie(...)

The Laravel 5.4 documentation doesn't anymore state queueing cookie as 5.0 doc does. Does it mean that the functionality has been removed in version 5.4 or did I make a mistake in my code?

Laravel 5.4 文档不再像 5.0 文档那样声明排队 cookie。这是否意味着该功能已在 5.4 版中删除,或者我在代码中犯了错误?

For sake of completeness, I'm using Dingo API package and the response is crafted it.

为了完整起见,我使用的是 Dingo API 包,并且响应是精心制作的。

Thank you for your help.

感谢您的帮助。

回答by Christian

I found that:

我找到:

Cookie queuing is not enabled for api requests, this is the reason why it didn't work.

没有为 api 请求启用 Cookie 排队,这就是它不起作用的原因。

I had to add in the middleware section of the appropriate file:

我不得不在相应文件的中间件部分添加:

protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

        //added below line at end of the array
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    ];

Open file App/Http/Kernel.phpadd the line \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,in protected $middlewarearray as displayed in above code snippet and test again it should work now.

打开文件App/Http/Kernel.php添加行\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,protected $middleware的显示在上面的代码片断并再次测试它应该现在的工作阵列。

回答by kmuenkel

In case anyone fond their way here by Google, one way for cookie inclusion to silently fail is if you're explicitly defining your domain variable in its creation, and forgot to remove the "http://" from the beginning of it first. That's not the case with OP, but it was what brought me here. ;)

万一有人喜欢 Google 的方式,cookie 包含失败的一种方法是如果您在创建时明确定义域变量,并且忘记首先从它的开头删除“http://”。OP 的情况并非如此,但正是它把我带到了这里。;)