📜 ⬆️ ⬇️

Magento 2: cookies, registry, session

Sometimes when processing requests in web applications, there is a need for short-term storage of intermediate information. A cookie mechanism is used to write information to the client’s browser, a registry is used to save data within one request, and a session is used to save data between requests. Under the cut - examples for Magento 2.


Cookies


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

Registry


The registry makes it possible to use the "global variables" in Magento (although the global variables themselves are not welcomed by the modern programmer community). The registry throws an exception when trying to write data to it with an already existing key (if the parameter $ graceful = false) or simply ignores overwriting (if $ 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); } } 

For some reason, Magento does not provide a method for rewriting any value in the registry, although, in my opinion, this function is a logical continuation of the register / unregister pair.


Session


SessionManagerInterface allows you to write and read data to / from the storage ( StorageInterface ). The storage is a DataObject , so get / set accessors are working for the session (through the __call magic method):


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

In this example, the default namespace is used to save data, so there is a chance that names will intersect with the developers of other modules. To reduce this probability, you can use prefixes in variable names, create miraculous method names like setYourCompanyNameReferralCode($data) , or use your own implementations of SessionManager and its associated SessionStorage with a different namespace.


PS
Thanks isxam for additions and clarifications.


')

Source: https://habr.com/ru/post/301186/


All Articles