没有设置cookie Symfony 3

scyqe7ek  于 2022-11-16  发布在  其他
关注(0)|答案(3)|浏览(148)

我试图找出在Symfony 3中设置cookie的正确方法。在阅读了这里的帖子后,我发现它是这样工作的;

$response = new Response();
$cookie = new Cookie("source", "$testing", time()+86400);
$response->headers->setCookie($cookie);

Response和Cookie都是HttpFoundation组件,但是在基本控制器中设置后;

/**
* @Route("/", name="homepage")
*/
    public function indexAction(Request $request)
    {
        $response = new Response();
        $cookie = new Cookie("source", "testing", time()+86400);
        $response->headers->setCookie($cookie);

        return $this->render('index.html.twig');
    }

在访问页面之后,根本就没有设置cookie;

我做错什么了吗?
有人在评论中要求一个$response的var_dump;

object(Symfony\Component\HttpFoundation\Response)#370 (6) {
  ["headers"]=> object(Symfony\Component\HttpFoundation\ResponseHeaderBag)#371 (5) {
    ["computedCacheControl":protected]=> array(2) {
      ["no-cache"]=> bool(true)
      ["private"]=> bool(true)
    }
    ["cookies":protected]=> array(1) {
      [""]=> array(1) {
        ["/"]=> array(1) {
          ["source"]=> object(Symfony\Component\HttpFoundation\Cookie)#372 (9) { 
            ["name":protected]=> string(6) "source"
            ["value":protected]=> string(7) "testing"
            ["domain":protected]=> NULL
            ["expire":protected]=> int(1495910350)
            ["path":protected]=> string(1) "/"
            ["secure":protected]=> bool(false)
            ["httpOnly":protected]=> bool(true)
            ["raw":"Symfony\Component\HttpFoundation\Cookie":private]=> bool(false)
            ["sameSite":"Symfony\Component\HttpFoundation\Cookie":private]=> NULL
          }
        }
      }
    }
    ["headerNames":protected]=> array(2) {
      ["cache-control"]=> string(13) "Cache-Control"
      ["date"]=> string(4) "Date"
    }
    ["headers":protected]=> array(2) {
      ["cache-control"]=> array(1) {
        [0]=> string(17) "no-cache, private"
      }
      ["date"]=> array(1) {
        [0]=> string(29) "Fri, 26 May 2017 18:39:10 GMT"
      }
    }
    ["cacheControl":protected]=> array(0) { }
  }
  ["content":protected]=> string(0) ""
  ["version":protected]=> string(3) "1.0"
  ["statusCode":protected]=> int(200)
  ["statusText":protected]=> string(2) "OK"
  ["charset":protected]=> NULL
}
uttx8gqw

uttx8gqw1#

我想我已经弄明白了,Return需要一个响应,render函数给出一个完整的响应,为了放入一个cookie,我需要在返回函数之前把它添加到render生成的响应中,如下所示;

$response = $this->render('index.html.twig');
$cookie = new Cookie("source", "testing", time()+86400);
$response->headers->setCookie($cookie);

return $response;
xjreopfe

xjreopfe2#

您忘记发送您创建的响应。只需添加$response-〉send();在设置cookie之后。

/**
    * @Route("/", name="homepage")
    */
    public function indexAction(Request $request)
    {
        $response = new Response();
        $cookie = new Cookie("source", "testing", time()+86400);
        $response->headers->setCookie($cookie);
        $response->send();

        return $this->render('index.html.twig');
    }
gr8qqesn

gr8qqesn3#

Symfony 5.4版本

我能够存储cookie值,如下所示。

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;

$response = new Response();
//Setting Cookie
$response->headers->setCookie(new Cookie('name', 'artisan-bay', time() + (24 * 60 * 60))); // Expiry 1 Day

// Sending the response to the client
$response->send();

从Cookie读取值

$request->cookies->get('name');

要删除cookie,请将过期时间设置为过去的时间

$response->headers->setCookie(new Cookie('name', '', time() - 3600));

相关问题