如何在“laravel”中设置没有响应的cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22345442/
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 set cookie without response in "laravel"
提问by user3409565
I want to set cookie after doing all stuffs with this group of route But when I use "before filter", it will return a response and then stop doing Another thing.
我想在用这组路由完成所有事情后设置 cookie 但是当我使用“before filter”时,它会返回一个响应然后停止做另一件事。
What should I do?
我该怎么办?
This is my code
这是我的代码
Route::filter('setcookie',function() {
$test = Input::get('test',0);
$cookie = Cookie::forever('cookie',Input::get('test'));
return Response::make(View::make('pages.home'))->withCookie($cookie);
});
Route::group(array('before' => 'setcookie'),function()
{
Route::get('/', function() {
return View::make('pages.home');
});
Route::controller('productrest', 'ProductRestController');
Route::resource('product', 'ProductController');
});
回答by Needpoule
You can use the Cookie::queue
function of laravel.
你可以使用Cookie::queue
laravel的功能。
Route::filter('cookie',function(){
Cookie::queue('key', 'value', 5);
});
From Laravel doc :
来自 Laravel 文档:
Queueing A Cookie For The Next Response
If you would like to set a cookie before a response has been created, use the Cookie::queue() method. The cookie will automatically be attached to the final response from your application.
为下一个响应排队 Cookie
如果您想在创建响应之前设置 cookie,请使用 Cookie::queue() 方法。cookie 将自动附加到您的应用程序的最终响应中。