Drupal 8 and Drupal 9 set cookie and get cookie example with symphony HttpFoundation Component

Setting Cookies

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:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...