简体   繁体   中英

Cookies on Zend Framework 2

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??

Solved!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM