Magento 2 overload, overwrite controller
This tutorial shows you how to overload or overwrite controller for your Magento 2 shop. You do both, overloading or overwriting a controller depends on your needs, but lets see what different solutions you can produce. You will find a lot of nonsense articles about this topic, which create more files than you really need and are confusing.
Magento 2 overload, overwrite controller
First things first: overloading or overwriting controllers is really simple and can be done with only a few files and lines of code. Before you start, you need to identify a controller that should be extended or replaced. For this you can use template path hints. You will find you desired controller in Magento 2 core. If you are unsure if it is the correct one you may add a
1 2 | echo "Test"; die(); |
at the top of execute method and try if you get this output. Remember to remove this change afterwards!!!
Overwrite controller
To overwrite a Magento 2 controller you only need 4 files:
- MyCompany/MyModule/registration.php123456<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'MyCompany_MyModule',__DIR__);
This is a base registration file that is needed for every module. You simply need to change MyCompany and MyModule the strings that represents your module – you need to do that for all following files! - MyCompany/MyModule/etc/module.xml1234<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"><module name="MyCompany_MyModule" setup_version="0.0.1" /></config>
Module configuration xml file with only 2 information: module name and current version.
With this 2 files you have created a base module that can be activated or deactivated for your shop. It does currently nothing. Next you need to add 2 files for extending a controller:
- MyCompany/MyModule/etc/di.xml1234<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"><preference for="Magento\Newsletter\Controller\Subscriber\NewAction" type="MyCompany\MyModule\Controller\Newsletter"/></config>
This is an important file for overloading and overwriting classes. In preference tag you declare a controller you like to extend. In my case it is Magento\Newsletter\Controller\Subscriber\NewAction. This tag needs two classes, one source and one target. You can name any target class you like to overload this core controller. It can have any path you like, but if you use this for many controllers, it is a good idea to use a readable path and class name to identify this controller. In my case I create a new MyCompany\MyModule\Controller\Newsletter class. - MyCompany/MyModule/Controller/Newsletter.php123456789<?phpnamespace MyCompany\MyModule\Controller;class Newsletter extends \Magento\Newsletter\Controller\Subscriber\NewAction{public function execute() {//add your code here}}
This one does all the magic. The simplest possible solution is mentioned above. Newsletter class extends base code model and overwrites execute method – which is the only method for a controller that is called by frontend action. You need to place you code there.
Overload controller
Sometimes you need to add additional functionality and controllers base functionality should work as expected. Basically you need to create the same files as mentioned the overwrite method before. The only big difference is that execute must call base execute method too. So you need to add a super method call at the top or bottom of method.
Conclusion
There is a lot of confusion about overwriting and overloading Magento 2 controllers, but to overload or overwrite controller is really simple. This article shows you the simplest possible solution to start your module creation.
Hi,
Can you add an example for overload controller?
Thanks