Zend framework2 Zend框架2-Cookie概念

Zend framework2 Zend框架2-Cookie概念,zend-framework2,Zend Framework2,我经常冲浪。我想使用COOKIE分配和检索一个值。在ZF2中我能做什么?我看到了很多在cookie中赋值的例子。请解释如何从cookie中检索值。HTTP中的cookie(请参阅存储在请求中的内容,并在每次发出请求时发送)。响应可以将其他参数添加到已存在的cookie中 因此,cookie检索是通过请求完成的,要更新cookie,您可以使用响应。根据RFC 2109,您分别使用cookie头和设置cookie头。因此,您可以通过 $this->getRequest()->getHea

我经常冲浪。我想使用COOKIE分配和检索一个值。在ZF2中我能做什么?我看到了很多在cookie中赋值的例子。请解释如何从cookie中检索值。

HTTP中的cookie(请参阅存储在请求中的内容,并在每次发出请求时发送)。响应可以将其他参数添加到已存在的cookie中

因此,cookie检索是通过
请求
完成的,要更新cookie,您可以使用
响应
。根据RFC 2109,您分别使用
cookie
头和
设置cookie
头。因此,您可以通过

$this->getRequest()->getHeaders()->get('Cookie')->foo = 'bar';
或通过以下方式设置cookies:

$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
标题。查看所有可用的方法

略作总结:

$cookie = $this->getResponse()->getCookie();
$cookie->foo = 'bar';
$cookie->baz = 'bat';

$this->setDomain('www.example.com');
$this->setExpires(time()+60*60*24*30);

通过
$this->getResponse()->getCookie()
进行的cookie访问虽然有效,但冗长乏味。因此,我扩展了响应和请求类。如下所示:

'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 ();
    }
}
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/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\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 ();
    }
}
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);
    }
}
现在,我喜欢更容易获得饼干

$this->getRequest ()->cookie ('ptime');
$this->getRequest ()->cookie ('alarm', 'last');


非常感谢。但是,我如何设置cookie过期时间、域名和cookie路径。您可以为此创建一个新的SetCookie头,并将其分配给响应对象的头列表。例如:
$cookie=new SetCookie($name、$value、$expires、$path、$domain、$secure、$httpOnly、$maxAge、$version);$this->getResponse()->getHeaders()->addHeader($cookie)
@2plus查看我的编辑,我链接到所有修改器都可用的SetCookie类。我的答案中还添加了一些设置器的简短示例。我喜欢轻松访问Cookie。这就像用大炮杀死苍蝇一样。为什么不创建一个控制器插件,作为控制器和请求之间的代理呢?它可以不受限制地工作如果在其他地方(而不是控制器中)有cookie设置逻辑,则更少。