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: Request & Response Objects - 4.x , 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.