For people using CakePHP 4.x: Instead of using the cookie component, you now need to set a cookie to the Response
object and fetch it via the Request
object:
// Create cookie and set it to the Response object
$cookie = (new Cookie('name'))
->withValue('Larry');
$this->setResponse($this->getResponse()->withCookie($testCookie));
// Fetch a cookie's value from the Request object
$name = $this->getRequest()->getCookie('name'); // 'Larry'
Note that setting cookies will not work when using debug()
or other output (e.g. echo
), as headers cannot be sent if any output is already sent. You should get a warning about this as well.
For more information see this answer, or check the documentation on setting cookies and getting cookies
.