Magento 2 Cookies, Registry and Session: Tutorial for Developers

Choose Magento
1mid read January 23, 2022
Choose Magento
Magento 2 Cookies, Registry and Session: Tutorial for Developers

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:

Magento 2 Cookies

Magento 2 Cookies

Magento 2 Registry

It allows using 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 registry

Magento 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 Session

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


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

How useful was this post?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 16

No votes so far! Be the first to rate this post.

Get in Touch
Looking for a partner to grow your business? We are the right company to bring your webstore to success.
Table of contents
We use cookies to ensure that we give you the best experience on our website. If you continue, well assume that you are happy to receive all cookies on this website. More info