General

Decomposition of Magento Controllers (All You Need to Know)

Hello Magento folks,

Magento 2 is the ultimate existence of the leading enterprise for Ecommerce platforms and has been used by over 200,000 online retailers. Basically, Magento 2 offers a wide range of tools which covers all the needs of the retailer including the advanced digital marketing tools.

Why did Decomposition need?

Mainly, in the latest release of Magento 2.4 it became the best chance to progress with all the backwards-incompatible changes that were waiting for years. After the speech of LBajsarowicz spoken at the recent Magento Conferences decided to replace inheritance with composition. The major change was the Pull Request and its followup which was introduced by Vinai Kopp.

There is a major architectural change done in decomposition of controllers by Lena Orobei and vinai Kopp. Both have started delivering by continuous support of each other and a great encouragement of LBajsarowicz.

To implement new Controller Action

\Magento\Framework\App\ActionInterface

Benefits

No need to extend

For creating a fully functional action controller now there is no need of extending from class.

Example controller GET Action

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\PageFactory;

class MyController implements HttpGetActionInterface
{
    /** @var PageFactory */    protected $resultPageFactory;

    public function __construct(PageFactory $resultPageFactory)
    {
        $this->resultPageFactory = $resultPageFactory;
    }
    
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

Everyone shall have noticed that here they have used \Magento\Framework\App\Action\HttpGetActionInterface. It is a method-specific Interface extending ActionInterface. If you want to explicitly define what methods are going to be handled by the Controller, the most common interfaces are:

\Magento\Framework\App\Action\HttpDeleteActionInterface

\Magento\Framework\App\Action\HttpGetActionInterface

\Magento\Framework\App\Action\HttpPostActionInterface

\Magento\Framework\App\Action\HttpPutActionInterface

Please be aware that the HEAD method is handled the same way that GET is.

Note: Changes to Magento core are fully backwards-compatible, so you still have time to migrate from Inheritance to Composition.

Conclusion

Hence, by going through the above article you may get all the rich and valuable information regarding the Decomposition of Magento Controllers provided by Magento 2.

Happy Reading?

Click to rate this post!
[Total: 7 Average: 4.7]
Gaurav Jain

Gaurav Jain is Co-Founder and Adobe Certified Expert-Magento Commerce Business Practitioner. Being Computer Engineer?‍? and possessing Extensive Marketing skills he handles all kinds of customer Queries and his Happy? & Helping? Nature makes customer's day Delightful. When he isn’t working, you’ll find Gaurav Reading on Books? or Traveling?. Also, he is Speaker at Magento Meetups.

View Comments

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

1 day ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

3 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

5 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago