Skip to main content

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);
    }
}