laravel 如何忘记laravel中的所有cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26059179/
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
How to forget all cookies in laravel?
提问by Brendan White
Does anyone know if there's a way to forget all cookies (well, all for my web app) in Laravel? I know that I can get rid of one by doing Cookie::forget('mycookie')
but what if I have a dozen or so, and I want to forget them all?
有谁知道是否有办法忘记 Laravel 中的所有 cookie(好吧,全部用于我的网络应用程序)?我知道我可以通过这样做来摆脱一个,Cookie::forget('mycookie')
但如果我有十几个,我想把它们都忘记怎么办?
Is there a simple one-line command like Cookie::forgetAll()
or something? Or do I just need to loop through all my cookies and forget them one by one?
是否有一个简单的单行命令之类的Cookie::forgetAll()
?还是我只需要遍历我所有的 cookie 并一一忘记它们?
Thanks!
谢谢!
PS - I'm using Laravel 4.1.28 but would happily update to the latest version if it will make any difference.
PS - 我正在使用 Laravel 4.1.28 但如果它会有所作为,我很乐意更新到最新版本。
回答by Ali
For deleting multiple cookies at once you can simply do the following and of course you can put it in a loop:
要一次删除多个 cookie,您只需执行以下操作,当然您可以将其放入循环中:
Cookie::queue(Cookie::forget('key1'));
Cookie::queue(Cookie::forget('key2'));
return $myResponse;
By doing this, there's also no need to return your response with the cookie object anymore.
通过这样做,也不再需要用 cookie 对象返回您的响应。
queue()
method queues up the result with cookies and adds them to the header of the request. It doesn't matter you're creating cookies or deleting them. It just works either way!
queue()
方法使用 cookie 将结果排入队列并将它们添加到请求的标头中。您是创建 cookie 还是删除 cookie 都没有关系。无论哪种方式都有效!
Also checkout this post here: https://coderwall.com/p/3etfgw/deleting-cookies-in-laravel
还可以在这里查看这篇文章:https: //coderwall.com/p/3etfgw/deleting-cookies-in-laravel
Thanks to JP Camarafor such a great post.
回答by lowerends
Looking at the Laravel source code (https://github.com/laravel/framework/blob/ef0864242e6e6f19a78f941b0710c844016ddf6e/src/Illuminate/Cookie/CookieJar.php), there doesn't seem to be a function to delete all the cookies at once, so you only option would indeed be to loop over all the cookies and delete them one by one.
查看 Laravel 源代码(https://github.com/laravel/framework/blob/ef0864242e6e6f19a78f941b0710c844016ddf6e/src/Illuminate/Cookie/CookieJar.php),似乎没有一次删除所有 cookie 的功能,因此您唯一的选择确实是遍历所有 cookie 并一一删除它们。