Site icon Qaisar Satti's Blogs

Magento 2 use registry

Magento 2 use registry

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');
}
Exit mobile version