Magento 2 Overriding Controller

This post walks through the process to override a controller in Magento 2.

There are a few steps to override a Magento 2 controller.

Step 1. Building a Magento 2 extension structure

Building directories as given following:

magento2 --- app --- code
                       |--- A2bizz --- Sample
                       | --- Controller
                       | --- etc
                              | --- module.xml
                              | --- di.xml

Creating module.xml to define a Magento2 extension:

<?xml version="1.0"?>
<!--
/**
 * Created by blog.a2bizz@gmail.com
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="A2bizz_Sample" 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 blog.a2bizz@gmail.com
*/
-->
<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="A2bizz\Sample\Controller\Index\Index" /> 
</config>

A2bizz\Sample\Controller\Index\Index 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/A2bizz/Sample/Controller, defining Index.php as following.

<?php
/**
 * Created by blog.a2bizz@gmail.com
 */
namespace A2bizz\Sample\Controller\Index;
class Index extends \Magento\Cms\Controller\Index\Index
{
    public function execute($coreRoute = null)
    {
        $this->messageManager->addSuccess('Message from new controller.');
        return parent::execute($coreRoute);
    }
} 

Index.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.