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.
\Magento\Framework\Registry $registry,
) {
$this->registry = $registry;
}
Register Registry Object
Now we use registry instance to register the registry object.
{
return $this->registry->register('test_var' , 'sometext');
}
get Registry Object
Now we use registry instance to get registry object.
{
return $this->registry->registry('test_var');
}
Unregister Registry Object
Now we use registry instance to unregister the registry object.
{
return $this->registry->unregister('test_var');
}