17

/**
 * 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

10

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
5

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

2
  • 1
    It is not ok because this helper is not marked with @api tag. The @api tag guarantees that public interface (methods) won't be changed in the future non-major updates.
    – MTP
    Nov 11, 2022 at 5:57
  • @MTP you may be right but there is no alternative to that at the moment, this is still better than using method that has been marked deprecated.
    – Timik
    Nov 22, 2022 at 4:11

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.