问题
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