2012-08-14 90 views
0

我試圖讓我的聯繫表單在表單提交後重定向到主頁。我設置我的模塊,並可以確認它正在工作。在下面的代碼中,我收到了「預先發送的」和「索引操作」日誌消息,但沒有得到「發佈操作」,正如您所期望的那樣,它在完成時也不會將我重定向到主頁。我確實收到聯繫電子郵件。誰能告訴我爲什麼前兩個功能正常工作,postAction()不是?Magento中的重寫控制器,一個功能不起作用

我將原始控制器中的所有代碼複製到我的控制器中進行故障排除。除了添加日誌消息和底部的重定向外,一切都是默認設置。

class MyCompany_Contacts_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(); 
    Mage::log('Pre-dispatched'); 

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

public function indexAction() 
{ 
    Mage::log('Index Action.'); 
    $this->loadLayout(); 
    $this->getLayout()->getBlock('contactForm') 
     ->setFormAction(Mage::getUrl('*/*/post')); 

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

public function postAction() 
{ 
    parent::postAction(); 
    Mage::log('Post Action.'); 
    $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 soon 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(''); 
    } 
} 

} 

config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
    <modules> 
    <MyCompany_Contacts> 
     <version>0.0.1</version> 
    </MyCompany_Contacts> 
    </modules> 

    <frontend> 
    <routers> 
     <contacts> 
     <args> 
      <modules> 
       <MyCompany_Contacts before="Mage_Contacts">MyCompany_Contacts</MyCompany_Contacts> 
      </modules> 
     </args> 
     </contacts> 
    </routers> 
    </frontend> 
</config> 
+1

你的config.xml是什麼樣的?張貼在這裏,人們將能夠獲得更多信息 – ivantedja 2012-08-14 17:16:44

回答

0

我想我明白了這一點。我回想起發佈數據在Akismet發送之前得到分析,因此完全有可能默認的Mage_Contacts已經得到擴展,並且首先會通過該模塊。在Akismet控制器中添加了日誌到postAction(),並對其進行了驗證。感謝您讓我走上正軌。

+1

這很好。我會問是否已被其他模塊覆蓋,但我認爲它不會發生。 – ivantedja 2012-08-14 18:16:11

+1

很高興聽到。祝你好運與模塊的其餘部分!請將答案標記爲正確答案,以便其他人可以從問題概述中看到此線索/問題已解決。 – 2012-08-14 18:16:37

1

問題就出在你的自定義postAction內parent::postAction();部分。你現在正在做的是發佈一個表單到/ post。它確實在postAction中結束,但直接通過parent :: postAction()進行路由。

父方法,因此Mage_Contacts_IndexController::postAction()還包含發送電子郵件的邏輯。因此你正在接受一個。問題是,在父方法結束時仍然有重定向$this->_redirect('*/*/');。這可以防止代碼到達你的`Mage :: log('Post Action')和你的其他自定義代碼。

解決方案:刪除parent::postAction(),您的自定義代碼在postAction方法將被執行,並最終您自己的重定向到主頁將運行。

+0

@ivantedja - 添加我的配置 – jkphl 2012-08-14 17:24:39

+0

感謝您的迴應。刪除了parent :: postAction(),但它仍然重定向到http://mydomain.com/contact/index。 – jkphl 2012-08-14 17:25:23

+0

它現在執行你的'post action'記錄嗎? – 2012-08-14 17:40:02

相關問題