Understanding that each business has its own story, to give a personalized touch to store always ends up with customization & development. Over the time Magento has topped Ecommerce CMS list with its functionality, features and power of extending the native functionalities. Compared to Magento 1, In Magento 2 overriding and manipulating with classes is becoming easier a developer to play quickly. At MageComp, we have never recommended modifying default core functionalities, but we can extend core functionalities using concepts of overriding which is a safe and easy way to achieve customization or development.
The extension plays an important role to achieve customization in Magento 2 but when there are tons of extensions installed within a store environment it creates conflicts with other extensions. At that time to quickly get rid of the problem, the easiest solution comes to mind is to override custom extension controller file. By default, there are three different ways to override core functionalities. Today we will use preference method to override any custom module controller into your extension.
Firstly, we need to notify Magento for overriding functionality by creating ‘di.xml’ at below path.
app\code\Vendor\Extension\etc\di.xml
1 |
<!--?xml version="1.0"?--> |
app\code\Vendor\Extension\Controller\Adminhtml\Customcontroller\Customaction.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!--?php namespace Vendor\ Extension \Controller\Adminhtml\Customcontroller; use Magento\Backend\App\Action\Context; class Customaction extends \Customvendor\Customextension\Controller\Adminhtml\Customcontroller\Customaction { public function __construct(Context $context) { // YOU NEED TO PASS ALL THE OBJECT WHICH USER BY PARENT CONTROLLER CONSTRUCT FUNCTION. parent::__construct($context); } protected function Customaction() { // ADD YOUR CUSTOM CODE HERE } } </pre> <p>By Implementing these code, you have successfully override custom extension controller file. You can add or change code logic according to your requirement of overriding controller file.<br ?--> If you face any issue while implementing this code comment down below. We will help you to fixed it asap. Happy Coding! |