Zend Framework 2 - Cookie概念

时间:2013-01-10 17:13:22

标签: zend-framework2

我已经冲浪了很多次。我想使用COOKIE分配和检索值。我怎么能在ZF2做?我看到很多用于在cookie中分配值的示例。请解释如何从cookie中检索值。

2 个答案:

答案 0 :(得分:16)

HTTP中的Cookie(请参阅RFC 2109只是存储在请求中的内容,并在每次发出请求时发送。响应可以添加其他参数,以便另外存储到已有的饼干。

所以Cookie 检索是通过Request完成的,更新您使用Response的Cookie。根据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';
}

请注意,CookieSet-Cookie标头会实现ArrayObject对象。要检查请求中是否存在cookie,您可以使用offsetExists

if ($cookie->offsetExists('foo')) {
    $param = $cookie->offsetGet('foo');
}

/更新:

如果要修改cookie的属性,您也可以修改Set-Cookie标题。查看所有可用方法at the class on Github

略微总结:

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

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

答案 1 :(得分:4)

通过$this->getResponse()->getCookie()访问Cookie的工作方式有效,但是很冗长且令人厌烦。所以我做了什么,我扩展了Response和Request类。这就是它的样子:

'service_manager' => array (
    'factories' => array (
        'Request' => 'Application\Mvc\Request\Factory',
        'Response' => 'Application\Mvc\Response\Factory',
    )
);

模块/应用/ SRC /应用/ MVC /请求/ 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 ();
    }
}

模块/应用/ SRC /应用/ MVC /请求/ 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;
    }
}

模块/应用/ SRC /应用/ MVC /响应/ 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 ();
    }
}

模块/应用/ SRC /应用/ MVC /响应/ 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 ());