为什么Cookie没有在Laravel Lumen中设置 [英] Why cookie isn't set in Laravel Lumen

查看:1786
本文介绍了为什么Cookie没有在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆