Magento – Custom Contact Form – Override Controller

controllersmagento-1.9moduleoverrides

I am trying to add a contacts form on multiple cms/products pages. I copied the default contacts form phtml and pasted it into a new phtml file. However, submitting the form redirects to the contacts page when I want it to remain on the page they completed the form on. So I created a custom module in order to override the controller and change the redirect.

Here are my files:

/app/etc/modules/SiDev_ModalContacts.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <SiDev_ModalContacts>
            <active>true</active>
            <codePool>local</codePool>
        </SiDev_ModalContacts>
    </modules>
</config>

/app/code/local/SiDev/ModalContacts/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <SiDev_ModalContacts>
            <version>0.1.0</version>
        </SiDev_ModalContacts>
    </modules>

    <frontend>
        <routers>
            <modalcontacts>
                <use>standard</use>
                <args>
                    <module>SiDev_ModalContacts</module>
                    <frontName>modalcontacts</frontName>
                </args>
            </modalcontacts>
        </routers>
    </frontend>
</config>

/app/code/local/SiDev/ModalContacts/controllers/IndexController.php:

<?php

require_once Mage::getModuleDir('controllers', 'Mage_Contacts').DS.'IndexController.php';

class SiDev_ModalContacts_IndexController extends Mage_Contacts_IndexController
{
    public function postAction()
    {
        $post = $this->getRequest()->getPost();
        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);

                $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();
                }
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
                    );

                if (!$mailTemplate->getSentSuccess()) {
                    throw new Exception();
                }

                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as quickly as possible. Thank you for contacting us.'));
                $this->_redirect('*/*/');

                return;
            } catch (Exception $e) {
                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }

        } else {
            $this->_redirect('*/*/');
        }
    }
}
?>

And for the form, I put:

<form id="contactForm" class="scaffold-form" action="<?php echo $this->getUrl('modalcontacts/index/post') ?>" method="post">

Upon submitting this form, I am sent to example.com/modalcontacts/index/post (not my real website), which is a 404 page, and no email is sent. I checked in the magento backend and it recognizes SiDev_ModalContacts as a module and it is enabled. And I have refreshed/flushed the magento cache from the backend. It seems to not recognize that my controller is there. As far as I'm aware, the correct method to set the form action to the controller is "frontName/controller/action" which would in fact be "modalcontacts/index/post", but it doesn't work. What am I missing?

Thank you for any help.

Best Answer

You need to add config.xml:

<?xml version="1.0"?>
<config>
<modules>
    <SiDev_ModalContacts>
        <version>0.1.0</version>
    </SiDev_ModalContacts>
</modules>

<frontend>
    <routers>
        <modalcontacts>
            <use>standard</use>
            <args>
                <module>SiDev_ModalContacts</module>
                <frontName>modalcontacts</frontName>
            </args>
        </modalcontacts>
    </routers>
</frontend>
 <global>
    <rewrite>        
        <sidev_modalcontacts_contacts_indexcontroller>
            <from><![CDATA[#^/contacts/index/#]]></from> <!-- Mage_Contacts_IndexController  -->
            <to>/modalcontacts/contacts_index/</to> <!-- SiDev_ModalContacts_Contacts_IndexController  -->
        </sidev_modalcontacts_contacts_indexcontroller>
    </rewrite>
    </global>
</config> 

Your controller code as below:

app\code\local\SiDev\ModalContacts\controllers\Contacts\IndexController.php

<?php
require_once "Mage/Contacts/controllers/IndexController.php";  
class SiDev_ModalContacts_Contacts_IndexController extends Mage_Contacts_IndexController{
 //your code goes here
}
Related Topic