我冲浪了很多。我想使用 COOKIE 分配和检索一个值。ZF2怎么办?我看到了很多在 cookie 中赋值的例子。请解释如何从 cookie 中检索值。
2 回答
HTTP 中的 cookie(参见RFC 2109只是存储在请求中的内容,并在每次发出请求时发送。响应可以添加其他参数以额外存储到现有的 cookie 中。
因此 cookie检索是通过Request
, 来更新您使用的 cookie 完成的Response
。根据 RFC 2109,您分别使用Cookie
标头和Set-Cookie
标头。因此,您可以通过以下方式直接访问这些标头
$this->getRequest()->getHeaders()->get('Cookie')->foo = 'bar';
或通过以下方式设置 cookie:
$this->getResponse()->getHeaders()->get('Set-Cookie')->foo = 'bar';
事情变得容易了一些,因为在请求和响应中有一个代理可以直接访问 cookie:
public function fooAction()
{
$param = $this->getRequest()->getCookie()->bar;
$this->getResponse()->getCookie()->baz = 'bat';
}
请记住Cookie
和Set-Cookie
标头实现ArrayObject
对象。要检查请求中是否存在 cookie,您可以使用offsetExists
:
if ($cookie->offsetExists('foo')) {
$param = $cookie->offsetGet('foo');
}
/更新:
如果您想修改 cookie 的属性,您也在此处修改Set-Cookie
标头。查看Github 上的类,了解所有可用的方法。
一个小小的总结:
$cookie = $this->getResponse()->getCookie();
$cookie->foo = 'bar';
$cookie->baz = 'bat';
$this->setDomain('www.example.com');
$this->setExpires(time()+60*60*24*30);
通过 cookie 访问$this->getResponse()->getCookie()
有效,但冗长且令人厌烦。所以我做了什么,我扩展了 Response 和 Request 类。这是它的样子:
'service_manager' => array (
'factories' => array (
'Request' => 'Application\Mvc\Request\Factory',
'Response' => 'Application\Mvc\Response\Factory',
)
);
模块/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 ();
}
}
模块/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;
}
}
模块/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 ();
}
}
模块/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);
}
}
现在,我可以更轻松地访问 cookie。
$this->getRequest ()->cookie ('ptime');
$this->getRequest ()->cookie ('alarm', 'last');
和
$this->getResponse ()->addCookie ('ptime', time ());