为什么Cookie没有在Laravel Lumen中设置 [英] Why cookie isn't set in Laravel Lumen
问题描述
此问题是以下此问题。
我有一个消息在我的看法谁说:这个网站使用cookie [...]关闭
。
当用户点击关闭
时,ajax请求被发送到控制器。函数如下:
public function acceptCookie(Request $ request)
{
if request-> valid =='accept'){
$ response = new Response('acceptCookie');
if($ response-> withCookie(cookie('acceptCookie','accepte',44000))){
return Response() - > json(array('statut'=& Succes'));
} else {
return Response() - > json(array('statut'=>'Erreur'));
}
} else {
return Response() - > json(array('statut'=>'Erreur'));
}
}
我没有任何错误,JSON返回总是 {statut:Succes}
为什么未设置Cookie?
根据Lumen文档,它看起来好像你需要排队cookie响应,你的例子。
如果你想在响应创建之前设置一个cookie,
使用Cookie :: queue()方法。
Cookie :: queue($ name,$ value,$ minutes);
p>我的建议是尝试替换withCookie排队cookie。但是,您可能需要重写该函数,以便适应,因为它看起来好像是试图发送到一个请求的响应。
希望这个工作为你!
This question is the following of this question.
I have a message in my view who says : This site uses cookie [...] Close
.
When user click on Close
, an ajax request is send to the controller. The function is the following :
public function acceptCookie(Request $request)
{
if ($request->valid == 'accept') {
$response = new Response('acceptCookie');
if ($response->withCookie(cookie('acceptCookie', 'accepte', 44000))) {
return Response()->json(array('statut' => 'Succes'));
} else {
return Response()->json(array('statut' => 'Erreur'));
}
} else {
return Response()->json(array('statut' => 'Erreur'));
}
}
I haven't any error and JSON returns always {"statut":"Succes"}
Why the cookie isn't set ?
Based on the Lumen documentation, it appears as though you need to queue the cookie for a response such as the one in your example. Here's what the docs say:
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::queue($name, $value, $minutes);
My suggestion would be to try replacing the withCookie with queuing the cookie instead. But, you might need to rewrite the function a bit in order to accomodate because it appears as though you're trying to send to responses from one request.
Hope this works for you!
这篇关于为什么Cookie没有在Laravel Lumen中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!