This post walks through the process to override a controller in Magento 2. Code is available at https://github.com/dbsdsun/magento2Override.
There are a few steps to override a Magento 2 controller.
Step 1. Building a Magento 2 extension structure
Building directories as following:
magento2 --- app --- code |--- Sample --- HelloWorld | --- Controller | --- etc | --- module.xml | --- di.xml
Creating module.xml to define a Magento2 extension:
<?xml version="1.0"?> <!-- /** * Created by wilson.sun330@gmail.com * Date: 13/05/2015 * Time: 5:02 PM */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Sample_HelloWorld" setup_version="0.0.1"/> </config>
Step 2. Setting preference in di.xml
Creating di.xml to refer the overriding class:
<?xml version="1.0"?> <!-- /** * Created by wilson.sun330@gmail.com * Date: 13/05/2015 * Time: 5:02 PM */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <preference for="Magento\Cms\Controller\Index\Index" type="Sample\HelloWorld\Controller\Index\ExtendIndex" /> </config>
Sample\HelloWorld\Controller\Index\ExtendIndex will be used to override Magento\Cms\Controller\Index\Index, which is the homepage in original Magento 2.
Step 3. Defining an overriding controller class
Under magento2/app/code/Sample/HelloWorld/Controller, defining ExtendIndex.php as following.
<?php /** * Created by wilson.sun330@gmail.com * Date: 13/05/2015 * Time: 5:02 PM */ namespace Sample\HelloWorld\Controller\Index; class ExtendIndex extends \Magento\Cms\Controller\Index\Index { public function execute($coreRoute = null) { $this->messageManager->addSuccess('Message from new controller.'); return parent::execute($coreRoute); } }
ExtendIndex.php redefines function execute() to override the function in \Magento\Cms\Controller\Index\Index. The new execute() adds a message to homepage, but it may also implements a complex logic.
I think directories building maybe was wrong. /etc and /controller same level with /sample?
Very good. thank you so much
To rewrite controller, you can do it by using preference. It mean that you need to put a rule in your router config using before attribute.
Open Mageplaza/HelloWorld/etc/di.xml insert the following block of code inside <config> tag rewrite controller in Magento 2
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <router id="standard"> <route id="mageplaza" frontName="hello"> <module name="Mageplaza_HelloWorld" before="Magento_Customer" /> </route> </router> </config>
This will completely change controller/action of module Magento_Customer with your controller code, so you should extend rewrited controller and make a change on the function which you want. Also, the controller and action in your module must have same name with rewrited controller/action. For example, if you want to rewrite controller:Magento\Customer\Controller\Account\Create.php
You have to register a router like above and create a controller:
NameSpace\ModuleName\Controller\Account\Create.php