Magento 2.4.4 Register Deprecated – How to Handle

backendmagento-1.9magento2.4PHPregister

I recently noticed the line $this->registry->register with register striked through. Mouseover displayed a message saying Register is deprecated. Could you please direct me to what is the alternative to this now? Thank you all. I am using the code below in the custom module.

    case "stripe_payments":
        // We need to set the current_order in registry, as StripeIntegration script info.php uses this registry.
        // Instantiate the StripeIntegration Info class after setting the registry.
        $this->registry->**register**('current_order', $order);
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $stripePayment = $objectManager->get('StripeIntegration\Payments\Block\Adminhtml\Payment\Info');

        // Now, we can make the calls to retrieve the Stripe details for the Credit card.
        $this->get_credit_card_payment_info($order_info, $stripePayment->getCard());
        $order_info["cc_name"] = $order->getBillingAddress()->getName();
        $order_info["cc_veri"] = $order->getPayment()->getTransactionId();
        break;

Best Answer

There is a comment in \Magento\Framework\Registry that states:

Registry usage as a shared service introduces temporal, hard to detect coupling into system. Its usage should be avoided. Use service classes or data providers instead.

Note that these deprecations warnings were added Mar 24, 2018. There are still thousands of uses in Magento's core modules.

I can't find any "officially" recommended alternatives.

Searching the web I found:

  1. https://www.atwix.com/magento-2/alternatives-for-deprecated-registry-class-magento-2-3/#alternatives
  2. A similar question to yours, but from 2019 (which references 1): \Magento\Framework\Registry deprecated in magento 2.3, How to create instance with Registry?

Between the ongoing, widespread use of registry in the core and the lack of recommended alternatives, I'm not personally concerned about using it for now. By the time it is actually removed, I expect there to be thousands of alternative implementations to review.

If that isn't acceptable, I think the key section from that first link is:

Where to get data from, if not Registry?

The short answer is “from the session”.

Related Topic