title: CakePHP 4.1.4 - How to create, read and check cookies in the new version of CakePHP
body: <p>In CakePHP 3.8 the segment of my controller looks like this:</p>
  <pre class="lang-php prettyprint-override"><code>// ...  
  
  public function beforeFilter(Event $event)
  {
      // ...
  
      $this-&gt;Cookie-&gt;configKey('guestCookie', [
          'expires' =&gt; '+1 days',
          'httpOnly' =&gt; true,
          'encryption' =&gt; false
      ]);
  
      if(!$this-&gt;Cookie-&gt;check('guestCookie')) {
          $guestID = Text::uuid();
          $this-&gt;Cookie-&gt;write('guestCookie', $guestID);
      }
      
      $this-&gt;guestCookie = $this-&gt;Cookie-&gt;read('guestCookie');
  
      // ...
  }
  
  
  public function error()
  {
      // ...
      $this-&gt;Cookie-&gt;delete('guestCookie');
      // ...
  }
  
  // ...
  </code></pre>
  <p>How to write the same thing in CakePHP4 version? My problem relates to defining Cookies.
  Cookie settings are described here:
  <a href="https://book.cakephp.org/4/en/controllers/request-response.html#cookie-collections" rel="nofollow noreferrer">https://book.cakephp.org/4/en/controllers/request-response.html#cookie-collections</a>
  , but unfortunately this didn't help me at all.</p>
  <p>I tried to solve the problem on this way:</p>
  <pre><code>    public function beforeFilter(EventInterface $event)
      {
  //...
      
          if(!$this-&gt;cookies-&gt;has('guestCookie')) { 
              $cookie = (new Cookie('guestCookie'))-&gt;withValue(Text::uuid())-&gt;withExpiry(new \DateTime('+20 days'))-&gt;withPath('/')-&gt;withSecure(false)-&gt;withHttpOnly(true); 
              $this-&gt;cookies = new CookieCollection([$cookie]);
          }
  
          $this-&gt;guestCookie = $this-&gt;cookies-&gt;get('guestCookie')-&gt;getValue();      
   //...       
      } 
  </code></pre>
  <p>In my case $ this-&gt;cookies-&gt;has('guestCookie') is always <strong>'false'</strong>.
  The cookie value is never stored in the browser.
  Please help.</p>