I created a new page with the CMS. I paste this in editor:

{{block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Theme::html/contact_form.phtml"}}

Then I overrided the contact_form.phtml successfully for my theme under


Submitting the form send to this controller vendor/module-contact/Controller/Index/Post.php

Route used in this controller is $this->_redirect('contact/index'); which is not the page I created with the cms.

My Question now is, how to override the controller?

I tried by creating the file under app/code/module-contact/Controller/Index/Post.php, but no success.

namespace Magento\Contact\Controller\Index;

class Post extends \Magento\Contact\Controller\Index
    public function execute()
        die('pass here?');
  • can you please put our override code?
    – Amit Bera
    Commented Jun 7, 2016 at 18:59
  • @arsenik how to change the subject for contact us form Commented Jul 10, 2018 at 7:26

1 Answer 1


You need to create small module for this.

Using di.xml can rewrite the controller Post.php

First ,Create module registration file registration.php at app\code\VendorName\ModuleName. Code is below:


Then create module main config module.xml file at app\code\VendorName\ModuleName\etc. Code is below:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="VendorName_ModuleName" setup_version="2.0.0">
            <module name="Magento_Contact"/>

3rd, create di.xml where you define configuration that which class will rewrite and which class rewriting and di.xml located at app\code\VendorName\ModuleName\etc and code is below:

<?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="VendorName\ModuleName\Controller\Index\Post" />

4th, write the rewrite class which located at Post.php app\code\VendorName\ModuleName\Controller\Index.

namespace VendorName\ModuleName\Controller\Index;

class Post extends \Magento\Contact\Controller\Index\Post
     * Post user question
     * @return void
     * @throws \Exception
    public function execute()
        $post = $this->getRequest()->getPostValue();
        if (!$post) {

        try {
            $postObject = new \Magento\Framework\DataObject();

            $error = false;

            if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
                $error = true;
            if (!\Zend_Validate::is(trim($post['comment']), 'NotEmpty')) {
                $error = true;
            if (!\Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                $error = true;
            if (\Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                $error = true;
            if ($error) {
                throw new \Exception();

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->_transportBuilder
                ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
                        'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                ->setTemplateVars(['data' => $postObject])
                ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
                ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))

                __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
            $this->_redirect('YOUR_CMS_PAGE_Identifier'); // change here 
        } catch (\Exception $e) {
                __('We can\'t process your request right now. Sorry, that\'s all we know.')
            $this->_redirect('YOUR_CMS_PAGE_Identifier');  // change here 
  • Now you need to enable php bin/magento module:status

    php bin/magento module:enable VendorName_ModuleName for enable the module.

  • and run php bin/magento setup:upgrade for enabled modules are properly registered.

Then clear cache.

NOte:This module is tested

  • VendorName\ModuleName can be MyThemes\2016?
    – arsenik
    Commented Jun 7, 2016 at 19:55
  • yes it can but note that location should be app/code
    – Amit Bera
    Commented Jun 7, 2016 at 20:04
  • what is AmitBera_CustomContacts ? in module.xml
    – arsenik
    Commented Jun 7, 2016 at 20:36
  • Answer updated.. now check
    – Amit Bera
    Commented Jun 7, 2016 at 20:39
  • hi @AmitBera i need one small help how to set custom subject for contact us form in the controller? Commented Jul 10, 2018 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.