Overriding Controller Class in Magento 2 Development

Posted by

In Magento 2, customization and extensibility are key aspects that allow merchants to tailor their e-commerce stores to their specific needs. One common customization requirement is to override a controller class, which allows you to modify the behaviour of specific controller actions without modifying the core code. In this article, we will provide a step-by-step guide on how to override a controller class in Magento 2 and implement your desired changes effectively.

Understanding Controller Override in Magento 2 development

Controllers in Magento 2 are responsible for handling various HTTP requests and processing the corresponding actions. While the core controllers provide the basic functionality, overriding them enables you to extend or modify the behaviour to suit your business requirements. By overriding a controller class, you can implement custom logic, add additional functionalities, or alter the response behaviour without directly editing the core files.

Step-by-Step Guide to Override a Controller Class

Create a Custom Module (Optional)

First, create a custom module to house your controller override. If you already have a custom module, you can skip this step.

Identify the Target Controller

Identify the core controller class you want to override. Locate the module and controller file path you wish to modify. For example, if you want to override the “View” module’s “Product” controller, the file path would be: Magento/Catalog/Controller/Product/View.php

Create a Controller Override

In your custom module, create a new controller class that extends the core controller you want to override. The new controller should be placed in the following file path: app/code/Learningmagento/Dynamicrow/Controller/Catalog/View .php.

namespace Learningmagento\Dynamicrow\Controller\Catalog;

class View extends \Magento\Catalog\Controller\Product\View
{
    public function execute()
    {
        .......
    }
}

Implement Your Custom Logic

Inside your custom controller class, implement your custom logic by either completely overriding the parent method or calling the parent method and extending its functionality.

Adding Sequence in module.xml

This is the part where most development forget about. In this you have to all the sequence in module.xml file. Modifying the core functionality won’t create any harm. It will work without module.xml but if you are overriding a class which is not a core but a custom plugin or third party plugin then it will create an issue if the load order in config.php is after our custom plugin. So in such case if module.xml is missing then your overridden controller won’t work. It is always recommended to use sequence in module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Learningmagento_Dynamicrow" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Declare Controller Override in di.xml

In your custom module’s di.xml file (located in app/code/Learningmagento/Dynamicrow/etc/di.xml), declare the controller override using the preference tag. This tells Magento to use your custom controller instead of the core controller.

Example of di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Controller\Product\View" type="Learningmagento\Dynamicrow\Controller\Product\View" />
</config>

Clear Cache and Test

After implementing the controller override and declaring it in di.xml, clear the Magento cache using the command php bin/magento cache:clean. Now, test your custom logic to ensure that the controller override is working as expected.

Conclusion

Overriding a controller class in Magento 2 development provides a flexible and non-intrusive way to customize the behaviour of specific controller actions. By following this step-by-step guide, you can easily implement your custom logic and extend the functionalities of your Magento 2 store without modifying the core code. Always remember to be cautious when making changes to core functionality and follow Magento’s best practices to ensure a stable and maintainable e-commerce store.

Leave a Reply

Your email address will not be published. Required fields are marked *