Override Magento Contacts Controller

限于喜欢 提交于 2019-12-29 09:09:38

问题


I'm trying to override Mage/Contacts/IndexController.php

I created a folder in local and created 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;
    }
}

I also put this code in 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>

I cleaned the cache, but my die; does not work,

Thanks for any help


回答1:


1. Best practices

Your config.xml file seems like this:

<?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. Bad practices

You can move your controller in app/local/Mage/Contacts/controllers/IndexController.php for a hard override.

And don't forget to enable your module in xml file in app/etc/modules directory




回答2:


Go through this, It overrides the IndexController.php from Mage_Contacts http://www.amitbera.com/how-to-override-a-controller-in-magento/




回答3:


IndexController.php file ( 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>

And Let`s go to create your sample code like this :

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



回答4:


Before getting to the answer I want to know have to defined your custom module in config.xml file.

I think that is missing here.

Add

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

after config node in config.xml file.

Also

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

it should be in small letters like below

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

Hope this will solve your problem.



来源:https://stackoverflow.com/questions/30477230/override-magento-contacts-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!