CMS 页面上的 Magento 联系表单显示 404 错误

Magento contact form on CMS page shows 404 error

我使用以下代码将我的联系人 form.phtml 添加到 cms 页面:

{{block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"}}

然而,当我提交联系表单时,它会显示 404 错误,因为 URL 被路由到 www.domain.de/contacts/index/post,因为我使用 URL 用商店代码 (de/en) 重写。

所以路由应该去www.domain.de/de/contacts/index/post

这仅在我按如下方式更改代码时有效:

{{block type="core/template" name="contactForm" form_action="http://domain.de/de/contacts/index/post" template="contacts/form.phtml"}}

因为我是 magento 的新手,所以我没有找到解决方案,我不知道这是否是 .htaccess 文件的问题。

有没有人遇到同样的问题并且知道如何解决?

更新:

只是为了确定我是否做对了: 我用以下内容创建了 \app\code\local\MARG\ContactForm\etc\config.xml\

<config>
    <modules>
        <MARG_ContactForm>
            <version>0.0.1</version>
        </MARG_ContactForm>
    </modules>
    <global>
        <blocks>
            <ContactForm>
                <class>MARG_ContactForm_Block</class>
            </ContactForm>
        <blocks>
    </global>
</config>

然后我创建了app\code\local\MARG\ContactForm\Block\ContactForm.php,内容为:

class MARG_ContactForm_Block_ContactForm extends Mage_Core_Block_Template
{
   protected function_construct()
   {
       $this ->setTemplate('contacts/form.phtml');
       parent::_construct();
   }

    public function getFormAction()
    {
        return Mage::getUrl('*/*/post', array('_secure' => $this->getRequest()->isSecure()));
    }
}

app\etc\modules\MARG_ContactForm.xml我放了内容:

<config>
    <modules>
        <MARG_ContactForm>
            <active>true</active>
            <codePool>local</codePool>
        </MARG_ContactForm>
    </modules>
</config>

我在 CMS 页面中添加了 {{block type="MARG/ContactForm" name="contactForm"}}

有没有我忽略的地方?

编辑:

我已经完全按照描述完成并添加 {{block type="ContactForm/ContactForm" name="contactForm"}} 到 CMS 页面。但是它仍然显示 exception.log 说

的空白页
exception 'Mage_Core_Exception' with message 'Invalid block type: Mage_ContactForm_Block_ContactForm' in C:\dev\plain\magento\app\Mage.php:595 Stack trace: 
#0 C:\dev\plain\magento\app\code\core\Mage\Core\Model\Layout.php(495): Mage::throwException('Invalid block t...') 
#1 C:\dev\plain\magento\app\code\core\Mage\Core\Model\Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance('ContactForm/Con...', Array)

这里的问题是该块的类型为 Mage_Core_Block_Template,它没有将表单操作转换为完全限定的 URL 的功能。通常这种情况发生在 Mage_Contacts_IndexController 中,但您的 CMS 页面当然没有使用该控制器。

解决方案是制作适合您目的的块类型。我假设您知道如何制作模块并且它的名称是 "example".

class Example_Example_Block_Contacts extends Mage_Core_Block_Template
{
    protected function _construct()
    {
        $this->setTemplate('contacts/form.phtml');
        parent::_construct();
    }

    public function getFormAction()
    {
        // copied from Mage_Contacts_IndexController::indexAction()
        return Mage::getUrl('contacts/index/post', array('_secure' => $this->getRequest()->isSecure()));
    }
}

这也使您的 CMS 页面更简单。只需要包括:

{{block type="example/contacts" name="contactForm"}}