How to use Registry Object in Magento 2.0

Magento 2.0 have a registry object that should help easy the transition for extensions. The class is Magento\Framework\Registry, and you can inject it in any constructo

public function __construct(//…
\Magento\Framework\Registry $registry,
//…
)
{
//…
$this->registry = $registry;
//…
}
and then set variables with

$this->registry->register(‘test_var’, ‘this is a test!’);
and fetch those variables back (even from a differnt object – Magento\Framework\Registry is a shared/singleton object)

echo $this->registry->registry(‘test_var’);

Leave a Reply