Magento中被重写的控制器,一个功能不工作
我正试图让我的联系表格重定向到主页后,表格提交。我设置了我的模块,可以确认它是否正常工作。在下面的代码中,我得到了“预调度”和“索引操作”日志消息,但没有得到“后操作”,正如您所料,它也不会在完成后将我重定向到主页。我确实正确地收到了联系电子邮件。有人能告诉我为什么前两个函数工作正常而postAction()工作不正常吗 我将原始控制器中的所有代码复制到控制器中,以便进行故障排除。除了添加日志消息和底部的重定向之外,所有内容都是默认的Magento中被重写的控制器,一个功能不工作,magento,controller,magento-1.4,Magento,Controller,Magento 1.4,我正试图让我的联系表格重定向到主页后,表格提交。我设置了我的模块,可以确认它是否正常工作。在下面的代码中,我得到了“预调度”和“索引操作”日志消息,但没有得到“后操作”,正如您所料,它也不会在完成后将我重定向到主页。我确实正确地收到了联系电子邮件。有人能告诉我为什么前两个函数工作正常而postAction()工作不正常吗 我将原始控制器中的所有代码复制到控制器中,以便进行故障排除。除了添加日志消息和底部的重定向之外,所有内容都是默认的 class MyCompany_Contacts_Index
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>
0.0.1
MyCompany_联系人
问题在于parent::postAction()代码>自定义postAction中的部件。您现在要做的是将表单发布到/发布。它确实会在您的postAction中结束,但随后会直接通过父::postAction()路由
因此,父方法Mage\u Contacts\u IndexController::postAction()
,也包含发送电子邮件的逻辑。因此,您将收到一个。问题是在父方法的末尾仍然存在重定向$this->\u重定向('*/*/')代码>。这会阻止代码到达“Mage::log('Post Action')和其他自定义代码
解决方案:删除parent::postAction()
,将执行postAction
方法中的自定义代码,并最终运行您自己的重定向到主页。我想我已经解决了这个问题。我记得post数据在发送之前由Akismet进行分析,因此完全有可能默认的Mage_联系人已经得到扩展,并首先通过该模块。将日志记录添加到Akismet控制器中的postAction(),并进行了验证。谢谢你让我走上正轨 您的config.xml是什么样子的?把它贴在这里,人们将能够得到更多的信息。谢谢你的回复。已删除父::postAction(),但它仍重定向到。它现在是否执行您的“post action”日志记录?不,只是检查了一下。其他两个仍然可以很好地记录。为了测试流,我将日志添加到core indexcontroller中,但它也没有记录任何内容。我刚刚将您的代码添加到干净的Magento 1.10安装中,代码确实起了作用。我只需要添加require_once'Mage/Contacts/controllers/IndexController.php'代码>位于控制器文件的顶部。在提交表格后,我正确地在主页上结束了。这很好。我想问一下它是否被其他模块覆盖了,但我认为不会发生。很高兴听到这个消息。祝模块的其余部分好运!请将答案标记为正确答案,以便其他人可以看到此线程/问题已从问题概述中解决。