This is the correct way of setting cookie.
To read cookie already written in the browser do:
$request->cookies->get('myCookie');
But after I created cookie in the $response object:
$cookie = new Cookie('myCookie', 'contentOfMyCookie');
$response = new Response();
$response->headers->setCookie($cookie);
I call this method:
$response->headers->getCookies();
I get an array of cookies, which are to be written in the browser - not those already existing there.
Figuratively, between $request and $response there is a time of executing controller's code.
Besides, in a twig template you can use
{{ app.request.cookies.get('myCookie') }}
you thus get value of the cookie already written in the browser, not that from the $response object! Newly created cookie from the browser you can read only after having reloaded page (ajax doesn't need to reload whole page).
To sum it up, you can read cookies using $request object, and create them with $response object. (Obviously, for some reasons, you can also read $response object cookies - but these are rather rare situations).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…