dqdjfb2325 2016-10-27 07:47
浏览 63

Magento IndexController覆盖不起作用

I am just trying to override app/code/core/Mage/Contacts/controllers/IndexController.php. But I think I am making mistake somewhere. I have already gone through all over similar questions in stackOverflow. And follow that. But the problem is still here.

This is my app/code/local/Namespace/Customcontacts/controllers/IndexController.php

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Customcontacts>
            <version>1.0.1</version>
        </Namespace_Customcontacts>
    </modules>
    <frontend>
        <routers>
            <customcontacts>
                <args>
                    <modules>
                        <Namespace_Customcontacts before="Mage_Contacts">Namespace_Customcontacts</Namespace_Customcontacts>
                    </modules>
                </args>
            </customcontacts>
        </routers>
    </frontend>
</config>

I have just copied the code from app/code/core/Mage/Contacts/controllers/IndexController.php to app/code/local/Namespace/Customcontacts/controllers/IndexController.php and print "Hello" in function indexAction().

<?php

class Namespace_Customcontacts_IndexController extends Mage_Contacts_IndexController
{

    const XML_PATH_EMAIL_RECIPIENT  = 'contacts/email/recipient_email';
    const XML_PATH_EMAIL_SENDER     = 'contacts/email/sender_email_identity';
    const XML_PATH_EMAIL_TEMPLATE   = 'contacts/email/email_template';
    const XML_PATH_ENABLED          = 'contacts/contacts/enabled';

    public function preDispatch()
    {
        parent::preDispatch();

        if( !Mage::getStoreConfigFlag(self::XML_PATH_ENABLED) ) {
            $this->norouteAction();
        }
    }

    public function indexAction()
    {   
        echo "Hello";
        // $this->loadLayout();
        // $this->getLayout()->getBlock('contactForm')
        //     ->setFormAction( Mage::getUrl('*/*/post', array('_secure' => $this->getRequest()->isSecure())) );

        // $this->_initLayoutMessages('customer/session');
        // $this->_initLayoutMessages('catalog/session');
        // $this->renderLayout();
    }

    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')->__('Thanks for getting in touch, we will reply to your message as soon as we can.'));
                $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('*/*/');
        }
    }

}

Please help me to overcome this problem..

  • 写回答

1条回答 默认 最新

  • dongyuan1984 2016-10-27 07:52
    关注

    First of all Check if your module is registered. `

    system->configuration->advance->disable modules output

    Then, In your config.xml file you have spell mistake

     <args>
         <modules>
           <Namesapce_Customcontacts before="Mage_Contacts">Namesapce_Customcontacts</Namesapce_Customcontacts>
         </modules>
     </args>
    

    should be

     <args>
         <modules>
           <Namesapce_Customcontacts before="Mage_Contacts">Namesapce_Customcontacts</Namespace_Customcontacts>
         </modules>
     </args>
    

    smilarly make the changes in node under modules section

    You need to include the controller file as

      <?php
        require_once 'Mage_Contacts_IndexController';
        class Namespace_Customcontacts_IndexController extends Mage_Contacts_IndexController
        {
    

    because controller's are not autoloaded

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测