Magento 2 Tutorial: Cookies, Registry, Session

Magento 2 Tutorial: Cookies, Registry, Session

30 May 2016

Sometimes when processing web application requests there is a need in short-term storage of intermediate information. There is an information storage in client’s browser we use cookies. For data storage within one request we use registry. And for data storage between requests – session.

Here are the examples for Magento 2:

Cookies

Magento 2 Tutorial: Cookies, Registry, Session

Registry

It allows to use global variables in Magento (although web developers do not welcome global variables). Registry throws an exception when attempting to record data with already existing key. If the option is $graceful = false or just ignores re-record if $graceful = true.

Magento 2 Tutorial: Cookies, Registry, Session

Session

SessionManagerInterface allows to record and read data in/from StorageInterface. Storage is a DataObject. Therefore, for sessions get/set accessors are used (via “magic” method_call):

Magento 2 Tutorial: Cookies, Registry, Session

In this example we use default namespace for data storage. Thus, there is a possibility of crossing the names of developers of other modules. In order to reduce this possibility we can add prefixes in variable names. It will help creating unique names for methods like

setYourCompanyNameReferralCode($data)

Otherwise, you can use SessionManager’s implementations with related to it SessionStorage with a different namespace.

 

Share to:    
Alina Kulpinska

Alina Kulpinska

Marketing manager and content writer of eLogic. In constant search of creative and innovative ideas for company's development.