-1

What is Registry in Magento2? Why use this. I want to know more detail about Registry. When we can use Registry in Magento2.

3
  • Don't ask the unnecessary question here, just google it. Commented Nov 21, 2018 at 5:23
  • @MilindSingh, What is unnecessary in the question? Commented 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. Commented Nov 21, 2018 at 5:28

2 Answers 2

0

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

https://alanstorm.com/magento-2-registry-object/

1
  • Can you expain here Commented Nov 21, 2018 at 4:23
0

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!

Not the answer you're looking for? Browse other questions tagged or ask your own question.