我試圖讓我的聯繫表單在表單提交後重定向到主頁。我設置我的模塊,並可以確認它正在工作。在下面的代碼中,我收到了「預先發送的」和「索引操作」日誌消息,但沒有得到「發佈操作」,正如您所期望的那樣,它在完成時也不會將我重定向到主頁。我確實收到聯繫電子郵件。誰能告訴我爲什麼前兩個功能正常工作,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>
你的config.xml是什麼樣的?張貼在這裏,人們將能夠獲得更多信息 – ivantedja 2012-08-14 17:16:44