我正在阅读 Laravel 的文档。我对设置真的很困惑
Laravel 中的 cookie
在队列中。此时Queueing A Cookie For The Next Response我很困惑,谁能告诉我一些可以应用这个的场景。
最佳答案
这很简单,如果您需要在收到响应之前设置 cookie,那么您应该将其排队,以便在发送响应时,cookie 将自动附加到响应中。例如,通常您会附加带有如下响应的 cookie
$response = Response::make('Hello World');
$response->withCookie(Cookie::make('name', 'value', $minutes));
现在,假设您手上还没有响应,并且想要在响应中附加一个 cookie,因此在这种情况下,您只需使用以下命令对 cookie 进行排队
Cookie::queue($name, $value, $minutes);
因此,每当发送响应时,排队的 cookie 都会附加到响应中,并自动与响应一起发送,换句话说,任何排队的 cookie 都会自动附加到响应中,因此您可以排队与响应一起发送的 cookie,每当发送响应时,排队的 cookie 也会被发送,并且您不必手动将 cookie 与响应附加在一起。
也许你可以这样想:
Step-1: You are in a before filter
Step-2: You determined to send a cookie with the response later so queue it here
Step-3: You are in the Controller and finally you did something like
return View:make(...);
Laravel 将检查是否有任何 cookie 排队,如果在队列中找到任何 cookie,则 Laravel 会将 cookie 附加到响应中,并自动与响应一起发送。
关于cookies - 在 Laravel 中,为下一个响应排队 Cookie 的场景是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20720808/