Override Magento Contacts Controller

后端 未结 4 418
自闭症患者
自闭症患者 2020-12-21 13:47

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

I created a folder in local and created Mynamespace/CustomContacts/controllers/IndexCont

相关标签:
4条回答
  • 2020-12-21 14:19

    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

    0 讨论(0)
  • 2020-12-21 14:36

    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');
    }
    }
    
    0 讨论(0)
  • 2020-12-21 14:39

    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.

    0 讨论(0)
  • 2020-12-21 14:41

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

    0 讨论(0)
提交回复
热议问题