当我创建一个cookie时,$ response对象可以完美地读取它,但是当刷新页面时,我得到空数组。在google上搜索了一些示例,发现它必须使用请求对象完成,但它根本不起作用。你有任何我缺少的想法吗?
public function indexAction(Request $request) {
$response = new Response();
var_dump($response->headers->getCookies()); //GIVES EMPTY ARRAY
var_dump($request->cookies->all()); //GIVES ONLY PHPSESSID, BUT NOT THE ONE CREATED
var_dump($request->cookies->get('user')); //GIVES NULL
// ... //
if ($loginForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$dql = // ... // ));
$result = $dql->getArrayResult();
if ($result) {
foreach ($result as $profile) {
$cookie = // ... //
$response->headers->setCookie(new Cookie('user', $cookie, $expire = 0, $path = '/', $domain = 'null', $secure = false, $httpOnly = false));
$response->sendHeaders();
}
var_dump($response->headers->getCookies()); //GIVES CORRECT COOKIE OBJECT
var_dump($request->cookies->all()); //GIVES ONLY PHPSESSID, BUT NOT THE ONE CREATED
var_dump($request->cookies->get('user')); //GIVES NULL
}
答案 0 :(得分:0)
您将域名设置为" null" (一个字符串),所以当你试图阅读它时,它没有出现,因为你不在" null"域。如果你想使用默认值,它应该只是null(不是字符串)。