Magento2 Override Contact Post Controller

by Karamdeep Singh   Last Updated July 18, 2018 06:09 AM

how to Override contact controller in custom module?

venoder/magento/module-contact/Controller/Index.php


Answers 1


create di.xml file in your custom module

/magento2/app/code/Contact/Custom/etc/di.xml 

add below code in di.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="\Magento\Contact\Controller\Index\Post" type="Contact\Custom\Controller\Index\ExtendIndex" />
</config>

create ExtendIndex.php file /magento2/app/code/Contact/Controller/Index/ExtendIndex.php

<?php
     namespace Contact\Custom\Controller\Index;
class ExtendIndex extends \Magento\Contact\Controller\Index\Post
{
    /**
     * Index action
     *
     * @return $this
     */
    public function execute($coreRoute = null)
    {


        $this->messageManager->addSuccess('Message from new controller.');
        return parent::execute($coreRoute);
    }
}
Karamdeep Singh
Karamdeep Singh
August 30, 2016 05:06 AM

Related Questions






Override method that Print Invoice

Updated April 14, 2017 11:09 AM