{"id":78,"date":"2018-04-10T12:37:39","date_gmt":"2018-04-10T12:37:39","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/04\/10\/use-registry-in-magento-2\/"},"modified":"2025-05-22T17:33:19","modified_gmt":"2025-05-22T12:03:19","slug":"use-registry-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/use-registry-in-magento-2\/","title":{"rendered":"How to Use Registry in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento registry stores data to memory which is specific to a particular request and for a duration of that request only. The Mage class is instantiated as a singleton object for every request and the instantiated Mage object remains in memory and is accessible in all classes until the request completes and the response is sent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 2 allows registering global variable that supports the static registry method. But, instead of using &nbsp;<strong><u>Mage :: register()<\/u><\/strong>&nbsp;and&nbsp;<strong><u>Mage::registry()<\/u><\/strong>&nbsp;in Magento 1, apply&nbsp;<strong>\/<u>Magento\/Framework\/Registry<\/u><\/strong>. It accepts the settings and the registry of the restored data. In this tutorial, we will learn to create and use your own custom registry in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Follow the below code to use Registry in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nprotected $_registry;\n\npublic function __construct(\\Magento\\Framework\\Registry $registry)\n{\n    $this->_registry = $registry;\n}\n\npublic function setRegistryVariable()\n{\n    $this->registry->register('customvar', 'Value');\n}\n\npublic function getRegistryVariable()\n{\n    return $this->registry->registry('customvar');\n}\n\npublic function unsetRegistryVariable()\n{\n    return $this->registry->unregister('customvar');\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I hope you readers got a proper guide on how to\u00a0<em><strong>use registry in Magento 2.\u00a0<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento registry stores data to memory which is specific to a particular request and for a duration of that request only. The Mage class is&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":15664,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions\/15664"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}