14

/**
 * Registry model. Used to manage values in registry
 *
 * Registry usage as a shared service introduces temporal, hard to detect coupling into system.
 * It's usage should be avoid. Use service classes or data providers instead.
 *
 * @api
 * @deprecated
 * @since 100.0.2
 */

https://www.mageplaza.com/devdocs/magento-2-registry-register.html

enter image description here

Who can show me a demo that use service classes or data providers instead.

thx.

2 Answers 2

9

Here you can find the way how to avoid usage of Registry - https://www.atwix.com/development/alternatives-for-deprecated-registry-class-magento-2-3/

2
2

The correct way of getting the current product on PDP page without using registry class is to use Catalog Helper class.

public function __construct(    
    \Magento\Catalog\Helper\Data $catalogHelper
) {
    $this->catalogHelper = $catalogHelper;
}

public function run() {
    $this->catalogHelper->getProduct();
}

By looking at the implementation you will see that it is still using registry class under the hood, but that is okay because when core class will be refactored to not use registry class, your code will not break and you should still get your product. This is the whole purpose of the helper class, essentially you are shifting responsibility to the framework from your own implementation, which is the best you can do in this scenario.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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