Magento 2 use registry

Today we talk about how in Magento 2 use registry. This tutorial includes register, registry, get registry value and unregister registry value. Registry is not a replacement of session in magento 2. Registry stores the value till the page loads. After the page loading the registry is unregister automatically. Registry can be helpful in many ways like you wish to register products Ids on one page from products collections and fetch them on another page , it’s so handy to work around registries. Following example will give you an insight to the actual implementation .So let’s start with our example registry example.

Registry Object

First we inject the registry class instance. So we can use registry in our class.

public function __construct(
   
    \Magento\Framework\Registry $registry,
   
) {
   
    $this->registry     = $registry;
   
}

Register Registry Object

Now we use registry instance to register the registry object.

public function registerValue()
{
 return $this->registry->register('test_var' , 'sometext');
}

get Registry Object

Now we use registry instance to get registry object.

public function getValue()
{
 return $this->registry->registry('test_var');
}

Unregister Registry Object

Now we use registry instance to unregister the registry object.

public function unSetValue()
{
   return $this->registry->unregister('test_var');
}

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply