无法设置新的Cookie

时间:2019-02-10 21:15:12

标签: symfony symfony-3.4

我正在尝试设置一个cookie以保存语言环境选择。
使用kernel.response事件,我做到了:

public function setCookie(FilterResponseEvent $event) {
    /** @var Response $response */
    $response=$event->getResponse();
    $cookie=new Cookie('_locale', $event->getRequest()->attributes->get('_locale'), strtotime('now + 1 year'), '/', $event->getRequest()->getHttpHost());
    $response->headers->setCookie($cookie);
}

_locale(如果不存在)是通过kernel.request事件设置的。

问题是在Web浏览器上收到响应后,没有设置Coockie。
它甚至不在响应转储中。

树枝转储

ParameterBag {#13 ▼
  #parameters: array:2 [▼
    "PHPSESSID" => "5aulgi7dbut4mfjmpgvc48cr2o"
    "REMEMBERME" => "QXBwQnVuZGxlXEVudGl0eVxVc2VyOmNISmxZMmxsYkM1bllXMWxjMEJuYldGcGJDNWpiMjA9OjE1NTAzMzM1OTM6ODFiNTllMmU0MDA4YjdiOWU1Y2Q0ZjJlN2E4Y2ExZWUxOTdkOWE1YjIxNDA0NDdmZTU2MGRj ▶"
  ]
}
HeaderBag {#16 ▼
  #headers: array:11 [▼
    "cookie" => array:1 [▼
      0 => "PHPSESSID=5aulgi7dbut4mfjmpgvc48cr2o; REMEMBERME=QXBwQnVuZGxlXEVudGl0eVxVc2VyOmNISmxZMmxsYkM1bllXMWxjMEJuYldGcGJDNWpiMjA9OjE1NTAzMzM1OTM6ODFiNTllMmU0MDA4YjdiOWU1Y2Q0ZjJlN2E4Y2ExZWUxOTdkOWE1YjIxNDA0NDdmZTU2MGRjOGFiMTAwOWZhZA%3D%3D ◀"
    ]
  ]
}

不过,“有趣的事实”是,如果我在dump($event); exit();之后添加$response->headers->setCookie($cookie);,则可以在event.response.headers中看到我的cookie

内核。响应转储

FilterResponseEvent {#995 ▼
  -response: Response {#903 ▼
    +headers: ResponseHeaderBag {#994 ▼
      #cookies: array:1 [▼
        "sphere.gdn" => array:1 [▼
          "/" => array:1 [▼
            "_locale" => Cookie {#154 ▼
              #name: "_locale"
              #value: "en"
              #domain: "sphere.gdn"
              #expire: 1581376164
              #path: "/"
              #secure: false
              #httpOnly: true
              -raw: false
              -sameSite: null
            }
          ]
        ]
      ]
    }
}

我的监听器事件是由Symfony找到的

Registered Listeners for "kernel.request" Event
===============================================

 ------- ------------------------------------------------------------------------------------------------- ---------- 
  Order   Callable                                                                                          Priority  
 ------- ------------------------------------------------------------------------------------------------- ---------- 
  #10     AppBundle\Service\LocaleHandlerService::setLocale()                                               0         
 ------- ------------------------------------------------------------------------------------------------- ----------

Registered Listeners for "kernel.response" Event
================================================

 ------- -------------------------------------------------------------------------------------------- ---------- 
  Order   Callable                                                                                     Priority  
 ------- -------------------------------------------------------------------------------------------- ---------- 
  #1      AppBundle\Service\LocaleHandlerService::setCookie()                                          0         
 ------- -------------------------------------------------------------------------------------------- ---------- 

有人可以告诉我设置我的cookie缺少什么吗?

1 个答案:

答案 0 :(得分:0)

尝试设置。

YES

阅读

$cookie=new Cookie('_locale', 'en', strtotime('now + 1 year'));