Cookie between controllers

Asked

Viewed 50 times

1

I created the following cookie in the Locale controller:

$cookie = new \Zend\Http\Header\SetCookie('localidade_id', $localidade->getIdentificador(), time() + 3600);         

However, when trying to access it in the Contact controller:

 $getcookie = $request->getCookie();
 $getcookie->offsetGet('localidade_id');

return is NULL. But if access is done in the Locale controller, I have a valid value.

It’s my first time using cookies with ZF2, so I don’t know if there’s anything wrong. If anyone can help me.

1 answer

2


I believe the problem is path (fourth parameter). By default, the cookie is set in the current directory: /localidade. You’d have to set him up for the whole domain: /, thus:

$cookie = new \Zend\Http\Header\SetCookie('localidade_id', $localidade->getIdentificador(), time() + 3600, '/');
  • Thank you. That was the problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.