Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Mage::registry() and Session in Magento

I am really confused about when to use Mage::registry() and Mage session.

Can any one suggest what is diff between both on them and when to use.

like image 717
Manoj Chaurasia Avatar asked Oct 14 '14 15:10

Manoj Chaurasia


2 Answers

The Magento registry is not persisted, as in once you are on a new page you will not see those registry variables still set. I mainly use the registry to communicate between controllers and blocks.

The session will persist, but know that there are multiple namespaces for sessions in Magento, and they will be cleared at certain times, like the checkout/session being cleared after the order is placed. It's best practice to create your own namespace for your session to avoid any conflicts like duplicate variables or clearing it at the wrong time.

As always Alan Storm has some good things to read on this topic:

http://alanstorm.com/magento_registry_singleton_tutorial

How to use Session in Magento

like image 132
Ryan Avatar answered Oct 04 '22 02:10

Ryan


Use Mage::registry() when you want to access variable in the SAME page request (e.g. passing variable from controller to template)

Use session when you want to access variables across DIFFERENT page requests (e.g. navigating from one page to another)

like image 35
MagExt Avatar answered Oct 04 '22 00:10

MagExt