0

I followed this and set it up in my AppController.php since the cookie should be created from wherever the user first hits the entire website:

use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;

class AppController extends Controller
{

    public function initialize()
    {
        parent::initialize();

        .....

        $cart_cookie = (new Cookie('cart'))
            ->withValue([])
            ->withExpiry(new \DateTime('+1 year'));

        $cookies = new CookieCollection([$cart_cookie]);
    }
}

Now how can I access this cookie in say CartController.php so I can modify its value? I tried accessing $this->cookies or $cookies but it says it doesn't exist. Maybe I missed something in the CakePHP book, but I'm not finding a solution or example.

yondaimehokage
  • 243
  • 2
  • 15
  • This still didn't work. I added the code it, but it says my cookie collection is an empty array. – yondaimehokage Jun 08 '18 at 16:37
  • Then you might have a different problem, it's impossible to tell without seeing the exact code that you are using. As a general rule when dealing with cookie problems, inspect the request and response in your browsers network console and check whether the expected cookie headers are present to figure whether they are being received/sent in the first place. – ndm Jun 09 '18 at 07:41

0 Answers0