CakePHP 4.1.4 - How to create, read and check cookies in the new version of CakePHP

In CakePHP 3.8 the segment of my controller looks like this:

...  

public function beforeFilter(Event $event)
{
...   
     
     $this->Cookie->configKey('guestCookie',['expires' => '+1 days','httpOnly' => true,'encryption' => false]); 
     if(!$this->Cookie->check('guestCookie')){
     $guestID=Text::uuid();
     $this->Cookie->write('guestCookie', $guestID);
     }
     $this->guestCookie=$this->Cookie->read('guestCookie');      
     
 ...       
    
}


public function error()
{
...   

$this->Cookie->delete('guestCookie');
... 
}
           
... 

How to write the same thing in CakePHP4 version? My problem relates to defining Cookies. Cookie settings are described here: https://book.cakephp.org/4/en/controllers/request-response.html#cookie-collections , but unfortunately this didn’t help me at all.

I tried to solve the problem on this way:

public function beforeFilter(EventInterface $event)
{

//…

    if(!$this->cookies->has('guestCookie')) { 
        $cookie = (new Cookie('guestCookie'))->withValue(Text::uuid())->withExpiry(new \DateTime('+20 days'))->withPath('/')->withSecure(false)->withHttpOnly(true); 
        $this->cookies = new CookieCollection([$cookie]);
    }

    $this->guestCookie = $this->cookies->get('guestCookie')->getValue();      

//…
}

In my case $ this->cookies->has(‘guestCookie’) is always ‘false’ . The cookie value is never stored in the browser. Please help.
Please help.

The last instruction in the section you linked reads:

Cookie objects can be added to responses:

// Add one cookie
$response = $this->response->withCookie($cookie);

// Replace the entire cookie collection
$response = $this->response->withCookieCollection($cookies);

Placing the cookie in the Response seems to be taking the place of the old write() process and you’ll need to add this step.

Thank you for your reply.