5

while trying to perform crud operation i came across

$this->coreRegistry->register(RegistryConstants::CURRENT_AUTHOR_ID, $authorId);

to perform edit action they got the id param and registered in core registry. I'm new to this and looking for logic and concept behind this... why do we need core registry in magento 2?

3
  • Can you get my answer? Commented Jul 17, 2017 at 8:28
  • i can understand your explanation, but still whats the need for such concept. Commented Jul 17, 2017 at 9:38
  • 1
    I describe in the answer that core registry is used to store global variable. we can also use session for that but registry is more secure than sessions. Commented Jul 20, 2017 at 15:55

2 Answers 2

6

Core registry is used to store global variable. You can store global variables via register() method and fetch value of variables via registry() method.

In Magento 1, it was possible to register a global variable with the static registry method.

Mage::register('some_var', 'some value');
var_dump(Mage::registry('some_var'));

Many extensions, include core Magento extensions, ended up using this from controller action methods to pass variables into the views.

While its future is uncertain (not marked explicitly supported via an @api, but not marked @deprecated) Magento 2 does have a similar registry object that should help easy the transition for extensions. The class is Magento\Framework\Registry, and you can inject it in any constructor.

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 different object – Magento\Framework\Registry is a shared/singleton object)

echo $this->registry->registry('test_var');

Source: magento-quickies.alanstorm

1
  • I describe in the answer that core registry is used to store global variable. we can also use session for that but registry is more secure than sessions Commented Jul 20, 2017 at 15:55
0

Registry pattern is basically a pattern that allows any object or data to be available in a public global scope for any resource to use.

In Magento, you can register an object or data with Mage::register function,

Mage::register('identifier', $object_or_data); After it is registered, you can call it with Mage::registry by passing identifier name:

Mage::registry('identifier'); You can also unregister an object at any time with Mage::unregister

Mage::unregister('identifier'); This is especially helpful transferring data between Models and Blocks without having to instantiate an entire class and load data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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