私はたくさんサーフィンをしました。COOKIE を使用して値を割り当てて取得したいと考えています。ZF2でどうすればいいですか?Cookie に値を割り当てる例をたくさん見ました。Cookie から値を取得する方法を説明してください。
2 に答える
HTTPのCookie(RFC 2109を参照してください。単に要求に保存され、要求が行われるたびに送信されます。応答は、既存のCookieに追加して保存される他のパラメーターを追加できます。
したがって、Cookieの取得は、を介して行われ、を使用してCookieRequest
を更新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',
)
);
モジュール/アプリケーション/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 ());