Magento 2:クッキー、レジストリ、セッション

Webアプリケーションでリクエストを処理するときに、中間情報を短期的に保存する必要がある場合があります。 Cookieメカニズムはクライアントのブラウザに情報を書き込むために使用され、レジストリは単一のリクエスト内のデータを保存するために使用され、セッションはリクエスト間のデータを保存するために使用されます。 猫の下-Magento 2の例







クッキー



CookieManagerInterface







class CookieHandler { const COOKIE_REFERRAL = 'referral'; protected $_cookieManager; public function __construct( \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager ) { $this->_cookieManager = $cookieManager; } public function getCookie() { $result = $this->_cookieManager->getCookie(self::COOKIE_REFERRAL, 'default value'); return $result; } public function setCookie($value) { // set public cookie (can be accessed by JS) $meta = new \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata(); $meta->setPath('/'); // use meta to define cookie props: domain, duration, security, ... $meta->setDurationOneYear(); $this->_cookieManager->setPublicCookie(self::COOKIE_REFERRAL, $value, $meta); // or set HTTP only cookie (is not accessible from JavaScript ) /** @var \Magento\Framework\Stdlib\Cookie\SensitiveCookieMetadata $meta */ $meta = null; // use meta to define cookie props: domain, duration, security, ... $this->_cookieManager->setSensitiveCookie(self::COOKIE_REFERRAL, $value, $meta); } }
      
      





登録



レジストリにより、Magentoで「グローバル変数」を使用できるようになります(ただし、グローバル変数自体は現代のプログラミングコミュニティでは歓迎されていません)。 レジストリは、既存のキーでデータを書き込もうとすると例外をスローします(パラメータ$ graceful = falseの場合)、または単に上書きを無視します($ graceful = trueの場合):







 class RegistryHandler { const REG_REFERRAL = 'referral'; protected $_registry; public function __construct( \Magento\Framework\Registry $registry ) { $this->_registry = $registry; } public function process() { // save value into the registry $value = 'u54321'; $graceful = true; // don't throw exception if variable with the same name exists $this->_registry->register(self::REG_REFERRAL, $value, $graceful); // get value from registry $registered = $this->_registry->registry(self::REG_REFERRAL); // replace value if ($this->_registry->registry(static::REG_REFERRAL)) { $this->_registry->unregister(static::REG_REFERRAL); } $this->_registry->register(static::REG_REFERRAL, $value); } }
      
      





何らかの理由で、Magentoはレジストリの値を上書きする方法を提供していませんが、私の意見では、この関数は「登録/登録解除」ペアの論理的な継続です。







セッション



SessionManagerInterfaceを使用すると、ストレージ( StorageInterface )との間でデータを読み書きできます 。 リポジトリはDataObjectであるため、セッションのアクセサを取得/設定するには、__ callマジックメソッドを使用します。







 class SessionHandler { protected $_sessionManager; public function __construct( \Magento\Framework\Session\SessionManagerInterface $sessionManager ) { $this->_sessionManager = $sessionManager; } public function process() { // save variable with name 'referral_code' into the session (using 'magic' methods) $this->_sessionManager->setReferralCode('u54321'); // restore saved value from the session $saved = $this->_sessionManager->getReferralCode(); } }
      
      





この例では、デフォルトの名前空間を使用してデータを保存しているため、他のモジュールの開発者と名前が交差する可能性があります。 この可能性を減らすために、変数名にプレフィックスを使用してsetYourCompanyNameReferralCode($data)



ような奇跡的なメソッド名を作成するか SessionManagerおよび関連するSessionStorageの独自の実装を異なる名前空間で使用できます。







PS

追加と説明をしてくれたisxamに感謝します。








All Articles