What is Registry in Magento2? Why use this. I want to know more detail about Registry. When we can use Registry in Magento2.
- Don't ask the unnecessary question here, just google it.Milind Singh– Milind Singh2018-11-21 05:23:04 +00:00Commented Nov 21, 2018 at 5:23
- @MilindSingh, What is unnecessary in the question?Chandra Kumar– Chandra Kumar2018-11-21 05:24:05 +00:00Commented Nov 21, 2018 at 5:24
- Because this is not Google. You need read dev docs and other magento dev guides for the knowledge. I see 2 more same type of questions here asked by you.Milind Singh– Milind Singh2018-11-21 05:28:49 +00:00Commented Nov 21, 2018 at 5:28
2 Answers
Please read this question's answer as your question is already asked here
what is the use of core registry in magento 2?
Alan Storm has written a great article on registry object see below link
- Can you expain hereChandra Kumar– Chandra Kumar2018-11-21 04:23:20 +00:00Commented Nov 21, 2018 at 4:23
Basically registry is used to store global variable You can store global variables via register() method and fetch value of variables via registry() method.
How to create a registry in magento 2
Here is the quick code snippet to help you work with Magento 2 registry objects
/** * @var \Magento\Framework\Registry */ protected $_registry; /** * ... * ... * @param \Magento\Framework\Registry $registry, */ public function __construct( ..., ..., \Magento\Framework\Registry $registry, ... ) { $this->_registry = $registry; ... ... } /** * Setting custom variable in registry to be used * */ public function setCustomVariable() { $this->registry->register('custom_var', 'Added Value'); } /** * Retrieving custom variable from registry * @return string */ public function getCustomVariable() { return $this->registry->registry('custom_var'); } /** * Return catalog product object * * @return \Magento\Catalog\Model\Product */ public function getProduct() { return $this->_registry->registry('product'); } /** * Return catalog current category object * * @return \Magento\Catalog\Model\Category */ public function getCurrentCategory() { return $this->_registry->registry('current_category'); } I hope it helps!