2014-07-01 28 views
0

表單在我當前的Magento部署中進行了自定義。自定義表單點擊contacts/index/post發送內容。Magento - 如何自定義聯繫我們POST操作?

現在,他們要求我自定義,因此當發生成功的表單發佈時,會發生自定義重定向。但是,這意味着編輯contacts/index/post入口點(並且小貓將被殺死)。

我已經選擇了替代路徑:在創建目標網址後將其更改爲新網址(例如customcontacts/index/post)。

我應該在哪裏創建這樣的入口點,以及在哪裏輸入以獲取contacts/index/post的內容(實際上是:代碼/腳本)(這樣我可以將代碼作爲基礎來創建我的自定義入口點)?

回答

1

如果有變化的行動,那麼你需要創建一個擴展which routing url is customcontacts並創建Indexcontroller.php並創造contolers行動postAction

更多細節如何創建一個模塊http://www.amitbera.com/create-an-magento-extension-with-custom-database-table/

爲你工作,修改config.xml的是

<?xml version="1.0" ?> 
<config> 
    <modules> 
     <Amit_Custommodule> 
      <version>1.0.0</version> 
     </Amit_Custommodule> 
    </modules> 
    <global> 

     <!-- start of block --> 
     <blocks> 
      <custommodule> 
       <class>Amit_Custommodule_Block</class> 
      </custommodule> 
     </blocks> 
    </global> 

    <!-- start of routers 
    --> 
    <frontend> 
     <routers> 
      <custommodule> 
       <use>standard</use> 
       <args> 
        <module>Amit_Custommodule</module> 
        <frontName>customcontacts</frontName> 
       </args> 
      </custommodule> 
     </routers> 
     </frontend> 
</config> 

indexController的代碼應該是

<?php 
class Amit_Custommodule_IndexController extends Mage_Core_Controller_Front_Action{ 


    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 ($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('*/*/'); 
     } 

    } 
} 

$此 - > _重定向( '/ /');根據你頁面的url

變化,如果窗體頁是聯繫人/索引/然後改變這種

$此 - > _重定向( '觸點/指數/') ; 爲了得到成功/失敗消息<?php echo $this->getMessagesBlock()->getGroupedHtml() ?> 表單頁面添加以下代碼

必須檢查

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

在形式上動作控制器存在 更

http://freegento.com/doc/d9/d7c/_contacts_2controllers_2_index_controller_8php-source.html

+0

TY:D一定要考它 –