我冲浪了很多次.我想使用COOKIE分配和检索值.我怎么能在ZF2做?我看到很多用于在cookie中分配值的示例.请解释如何从cookie中检索值.
Jur*_*man 16
HTTP中的cookie(请参阅RFC 2109,只是存储在请求中的内容,并在每次发出请求时发送.响应可以添加其他参数,以便另外存储到现有的cookie中.
所以cookie 检索是通过更新你使用的cookie Request
来完成的.根据RFC 2109,您分别使用标头和标头.因此,您可以通过直接访问这些标头Response
Cookie
Set-Cookie
$this->getRequest()->getHeaders()->get('Cookie')->foo = 'bar';
Run Code Online (Sandbox Code Playgroud)
或通过以下方式设置cookie
$this->getResponse()->getHeaders()->get('Set-Cookie')->foo = 'bar';
Run Code Online (Sandbox Code Playgroud)
事情变得更容易一些,因为在请求和响应中有一个代理直接访问cookie:
public function fooAction()
{
$param = $this->getRequest()->getCookie()->bar;
$this->getResponse()->getCookie()->baz = 'bat';
}
Run Code Online (Sandbox Code Playgroud)
请记住Cookie
,Set-Cookie
标头实现了ArrayObject
对象.要检查请求中是否存在cookie,您可以使用offsetExists
:
if ($cookie->offsetExists('foo')) {
$param = $cookie->offsetGet('foo');
}
Run Code Online (Sandbox Code Playgroud)
/更新:
如果要修改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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14018 次 |
最近记录: |