I have surfed a lot. I would like to assign and retrieve a value using a COOKIE. How can i do in ZF2? I saw a lot of examples for assigning value in cookie. Please explain that how to retrieve a value from cookie.
A cookie in HTTP (see RFC 2109 simply something stored in the request and send every time a request is made. A response can add other parameters to be stored additionally to the already existing cookies.
So the cookie retrieval is done via the Request
, to update a cookie you use the Response
. According to RFC 2109 you use respectively the Cookie
header and the Set-Cookie
header. You can thus directly access these headers via
$this->getRequest()->getHeaders()->get('Cookie')->foo = 'bar';
Or set cookies via:
$this->getResponse()->getHeaders()->get('Set-Cookie')->foo = 'bar';
Things are made a little bit easier though because there is a proxy at the request and response to directly access the cookie:
public function fooAction()
{
$param = $this->getRequest()->getCookie()->bar;
$this->getResponse()->getCookie()->baz = 'bat';
}
Keep in mind the Cookie
and Set-Cookie
headers implement the ArrayObject
object. To check whether a cookie is present in the request, you can thus use offsetExists
:
if ($cookie->offsetExists('foo')) {
$param = $cookie->offsetGet('foo');
}
/update:
If you want to modify properties of the cookie, you are also here modifying the Set-Cookie
header. Take a look at the class on Github for all the methods available.
A slight summary:
$cookie = $this->getResponse()->getCookie();
$cookie->foo = 'bar';
$cookie->baz = 'bat';
$this->setDomain('www.example.com');
$this->setExpires(time()+60*60*24*30);
The cookie access via $this->getResponse()->getCookie()
works but is long-winded and tiresome. So what I did, I extended Response and Request classes. Here what it looks like:
'service_manager' => array (
'factories' => array (
'Request' => 'Application\Mvc\Request\Factory',
'Response' => 'Application\Mvc\Response\Factory',
)
);
module/Application/src/Application/Mvc/Request/Factory.php
namespace Application\Mvc\Request;
use Zend\Console\Request as ConsoleRequest;
use Zend\Console\Console;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class Factory implements FactoryInterface
{
public function createService (ServiceLocatorInterface $serviceLocator)
{
if (Console::isConsole ())
{
return new ConsoleRequest ();
}
return new HttpRequest ();
}
}
module/Application/src/Application/Mvc/Request/HttpRequest.php
namespace Application\Mvc\Request;
use Zend\Http\PhpEnvironment\Request;
class HttpRequest extends Request
{
public function hasCookie ($name)
{
assert ('is_string($name)');
$cookie = $this->getCookie();
if (empty ($cookie))
{
return false;
}
if (isset ($cookie [$name]))
{
return true;
}
return false;
}
public function cookie ($name, $default = null)
{
assert ('is_string($name)');
if ($this->hasCookie($name))
{
$cookie = $this->getCookie();
return $cookie [$name];
}
return $default;
}
}
module/Application/src/Application/Mvc/Response/Factory.php
namespace Application\Mvc\Response;
use Zend\Console\Response as ConsoleResponse;
use Zend\Console\Console;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class Factory implements FactoryInterface
{
public function createService (ServiceLocatorInterface $serviceLocator)
{
if (Console::isConsole ())
{
return new ConsoleResponse ();
}
return new HttpResponse ();
}
}
module/Application/src/Application/Mvc/Response/HttpResponse.php
namespace Application\Mvc\Response;
use Zend\Http\PhpEnvironment\Response;
use Zend\Http\Header\SetCookie;
class HttpResponse extends Response
{
public function addCookie ($name, $value, $expires = null, $path = null, $domain = null, $secure = false, $httponly = false, $maxAge = null, $version = null)
{
$cookie = new SetCookie ($name, $value, $expires, $path, $domain, $secure, $httponly, $maxAge, $version);
$this->getHeaders ()
->addHeader ($cookie);
}
}
Now, I enjoy much easier access to cookies.
$this->getRequest ()->cookie ('ptime');
$this->getRequest ()->cookie ('alarm', 'last');
and
$this->getResponse ()->addCookie ('ptime', time ());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With