Cookies on Zend Framework 2

59 Views Asked by At

I'm trying to use cookies on ZF2 but I have problems.

My way to create a cookie is like that :

$cookie = new SetCookie('locale', $locale, time() + 365 * 60 * 60 * 24);
    $response = $serviceManager->get('Response')->getHeaders();
    $response->addHeader($cookie);

The problem is when I try to read that nothing is shown. I read the cookie like that:

$cookies = $serviceManager->get('Request')->getHeaders()->get('Cookie');
    $cookies->locale;

What's wrong??

1

There are 1 best solutions below

0
On BEST ANSWER

Solved!

The problem was the path I assigned to cookie was wrong!

$cookie = new SetCookie('locale', $locale, time() + 365 * 60 * 60 * 24, $request->getBaseUri());