为什么没有在 Laravel Lumen 中设置 cookie
Why cookie isn't set in Laravel Lumen
此问题是 的后续问题。
我认为有一条消息说:This site uses cookie [...] Close
。
当用户点击 Close
时,一个 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 returns 总是 {"statut":"Succes"}
为什么没有设置 cookie?
根据 Lumen 文档,您似乎需要将 cookie 排队等待响应,例如示例中的响应。文档是这样说的:
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);
我的建议是尝试将 withCookie 替换为排队 cookie。但是,您可能需要稍微重写该函数以便适应,因为它看起来好像您正在尝试从一个请求发送响应。
希望这对你有用!
基于 Illuminate\Http\ResponseTrait
行 28
,Illuminate\Http\Response::withCookie
方法返回 $this
。
/**
* Add a cookie to the response.
*
* @param \Symfony\Component\HttpFoundation\Cookie $cookie
* @return $this
*/
public function withCookie(Cookie $cookie)
{
$this->headers->setCookie($cookie);
return $this;
}
表示您的代码存在逻辑错误。
// This always return Illuminate\Http\Response instance,
// thus it will never reach ELSE statement forever.
if ($response->withCookie(cookie('acceptCookie', 'accepte', 44000))) {
return Response()->json(array('statut' => 'Succes'));
} else {
return Response()->json(array('statut' => 'Erreur'));
}
此问题是
我认为有一条消息说:This site uses cookie [...] Close
。
当用户点击 Close
时,一个 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 returns 总是 {"statut":"Succes"}
为什么没有设置 cookie?
根据 Lumen 文档,您似乎需要将 cookie 排队等待响应,例如示例中的响应。文档是这样说的:
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);
我的建议是尝试将 withCookie 替换为排队 cookie。但是,您可能需要稍微重写该函数以便适应,因为它看起来好像您正在尝试从一个请求发送响应。
希望这对你有用!
基于 Illuminate\Http\ResponseTrait
行 28
,Illuminate\Http\Response::withCookie
方法返回 $this
。
/**
* Add a cookie to the response.
*
* @param \Symfony\Component\HttpFoundation\Cookie $cookie
* @return $this
*/
public function withCookie(Cookie $cookie)
{
$this->headers->setCookie($cookie);
return $this;
}
表示您的代码存在逻辑错误。
// This always return Illuminate\Http\Response instance,
// thus it will never reach ELSE statement forever.
if ($response->withCookie(cookie('acceptCookie', 'accepte', 44000))) {
return Response()->json(array('statut' => 'Succes'));
} else {
return Response()->json(array('statut' => 'Erreur'));
}