2013-01-10 18 views
7

私は多くのサーフィンをしました。私はCOOKIEを使用して値を割り当てて取得したいと思います。私はZF2でどうすればいいですか?私は、クッキーに価値を割り当てるための多くの例を見ました。クッキーから値を取得する方法を説明してください。Zend Framework 2 - クッキーの概念

答えて

16

HTTPでクッキーが(リクエストに保存されているRFC 2109単に何かを参照し、要求が行われるたびに送信されます。だから、他のパラメータを追加することができ、応答既存のクッキーに加えて保存される。

クッキー検索は、RFC 2109によると、あなたは、それぞれCookieヘッダーとSet-Cookieヘッダーを使用しています。更新あなたがResponseを使用したクッキーに、Requestを介して行われます。あなたが直接

経由でこれらのヘッダにアクセスするようにすることができます
$this->getRequest()->getHeaders()->get('Cookie')->foo = 'bar'; 

または、設定されたクッキーを経由:直接クッキーにアクセスするための要求および応答でのプロキシがありますので

$this->getResponse()->getHeaders()->get('Set-Cookie')->foo = 'bar'; 

物事はいえ少し簡単に作られています:

public function fooAction() 
{ 
    $param = $this->getRequest()->getCookie()->bar; 

    $this->getResponse()->getCookie()->baz = 'bat'; 
} 

キープ念頭に置いてCookieSet-CookieヘッダーはArrayObjectオブジェクトを実装しています。クッキーがリクエストに存在しているかどうかを確認するには、あなたがこれoffsetExistsを使用することができます。

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

/更新:

クッキーのプロパティを変更したい場合は

は、あなたもここに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); 
+0

ありがとうございました。しかし、どのように私は、クッキーの有効期限の時間、ドメイン名とクッキーのパスを設定することができます。 – 2plus

+4

これに対して新しいSetCookieヘッダーを作成し、それを応答オブジェクトのヘッダーリストに割り当てることができます。例: '$ cookie = new SetCookie($ name、$ value、$ expires、$ path、$ domain、$ secure、$ httpOnly、$ maxAge、$ version); $ this-> getResponse() - > getHeaders() - > addHeader($ cookie); '。 –

+0

@ 2plus私の編集を見て、私はすべての修飾子が利用できるSetCookieクラスにリンクしました。いくつかのセッターの短い例が私の答えに追加されています。 –

4

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); 
    } 
} 

は今、私はクッキーにはるかに簡単にアクセスできます。

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

$this->getResponse()->addCookie ('ptime', time()); 
+2

クッキーに簡単にアクセスできます。 – Saeven

+1

これは、大砲でフライを殺すのと同じことです。コントローラーと要求の間のプロキシーとして動作するコントローラー・プラグインを作成しないでください。 –

+0

クッキー設定ロジックが他の場所(コントローラではない)に設定されていなければ動作します。 – akond