Categories
magento 2 Magento Developer Magento Expert

Rewrite controller in Magento 2

Create a custom module for magento2 and then create a di.xml file into the directory [Name Space]/[Your Module]/etc/di.xml

Add the following code

<?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\Cms\Controller\Index\Index” type=”[Name Space]\[Your Module]\Controller\Cms\Index” />

</config>

Now defining an overriding controller class

Create Index.php in app/code/[Name Space]/[Your Module]/Controller/Index

<?php

namespace [Name Space]\[Your Module]\Controller\Index;

class Index extends \Magento\Cms\Controller\Index\Index

{

public function execute($coreRoute = null)

{

// todo

}

}

If you facing any issue with please consult with our magento experts

Leave a Reply

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