覆盖 Magento 联系人控制器

Override Magento Contacts Controller

我正在尝试覆盖 Mage/Contacts/IndexController.php

我在本地创建了一个文件夹并创建了Mynamespace/CustomContacts/controllers/IndexController.php

<?php

require_once 'Mage/Contacts/controllers/IndexController.php';

class Mynamespace_CustomContacts_IndexController extends Mage_Contacts_IndexController {

    protected function indexAction () {
        die;
    }
}

我也把这段代码放在了Mynamespace/CustomContacts/etc/config.xml

<config>
    <frontend>
        <routers>
            <contacts>
                <args>
                     <modules>
                        <Mynamespace_CustomContacts before="Mage_Contacts">Mynamespace_CustomContacts</Mynamespace_CustomContacts>
                    </modules>
                </args>
            </contacts>
        </routers>
    </frontend>
</config>

我清理了缓存,但是我死了;不起作用,

感谢您的帮助

通过这个,它覆盖了 Mage_Contacts 中的 IndexController.php http://www.amitbera.com/how-to-override-a-controller-in-magento/

1。最佳实践

您的 config.xml 文件如下所示:

<?xml version="1.0"?>
<config>
    <modules>
        <Mynamespace_CustomContacts>
            <version>0.1.0</version>
        </Mynamespace_CustomContacts>
    </modules>
    <frontend>
        <routers>
            <contacts>
                <args>
                    <modules>
                        <Mynamespace_CustomContacts before="Mage_Contacts">Mynamespace_CustomContacts</Mynamespace_CustomContacts>
                    </modules>
                </args>
            </contacts>
        </routers>
    </frontend>
</config>

2。不良做法

您可以在 app/local/Mage/Contacts/controllers/IndexController.php 中移动您的控制器以进行硬覆盖。

并且不要忘记在 app/etc/modules 目录

的 xml 文件中启用您的模块

在得到答案之前,我想知道必须在 config.xml 文件中定义您的自定义模块。

我认为这里缺少。

添加

<modules>
        <Mynamespace_CustomContacts>
            <version>1.0.0</version>
        </Mynamespace_CustomContacts>
    </modules>

config.xml 文件中的配置节点之后。

还有

<Mynamespace_CustomContacts before="Mage_Contacts">
    Mynamespace_CustomContacts
</Mynamespace_CustomContacts>`

应该是像下面这样的小写字母

<mynamespace_customcontacts before="Mage_Contacts">
     Mynamespace_CustomContacts
</mynamespace_customcontacts>`

希望这能解决您的问题。

IndexController.php 文件 ( local/your_company/your_block_name/controllers/

<?php
require_once Mage::getModuleDir('controllers','Mage_Contacts').DS.'IndexController.php';

class IGN_Siteblocks_IndexController extends Mage_Contacts_IndexController
{
    public function indexAction()
    {

    }
}

local/your_company/your_block_name/etc/config.xml

<contacts>
                <args>
                    <modules>
                        <siteblocks before="Mage_Contacts">IGN_Siteblocks</siteblocks>
                    </modules>
                </args>
            </contacts>

让我们像这样创建您的示例代码:

class IGN_Siteblocks_IndexController extends Mage_Contacts_IndexController
{
   public function createAction()
{
    return $this->_redirect('noroute');
}
}