The response cookies can be implemented through the headers public attribute:
use Symfony\Component\HttpFoundation\Cookie;
$response->headers->setCookie(Cookie::create('foo', 'bar'));
The setCookie() method takes an instance of Cookie as an argument.
You can clear a cookie via the clearCookie() method.
In addition to the Cookie::create() method, you can create a Cookie object from a raw header value using fromString() method. You can also use the with*() methods to change some Cookie property (or to build the entire Cookie using a fluent interface). Each with*() method returns a new object with the modified property:
$cookie = Cookie::create('foo')
->withValue('bar')
->withExpires(strtotime('Fri, 20-May-2011 15:25:52 GMT'))
->withDomain('.example.com')
->withSecure(true);
n addition to the Cookie::create() method, you can create a Cookie object from a raw header value using fromString() method. You can also use the with*() methods to change
Drupal 8 cookies can be set using ResponseHeaderBag from the Symfony\Component\HttpFoundation\Response object.
Set new cookie value
use Symfony\Component\HttpFoundation\Cookie;
$cookie = new use Cookie('cookie_name', TRUE);
$response->headers->setCookie($cookie);
return $response;
Get cookies value
$request->cookies->get('cookie_name');
No comments:
Post a Comment