Try the Demo

Magento

eCommerce Software for Online Growth

Magento Forum

Our new hosted solution for small & emerging businesses
   
Page 1 of 3
Poll
Was this sample helpful?
Yes 23
No 7
Total Votes: 30
You must be a logged-in member to vote
Sample Usage: Overriding a core controller
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 


If you find that you want to change the behaviour of a core controller, say Mage_Customer_AccountController, then prior to this module, you had to use url rewriting.  Now, using LKC_ModularRouters, you can treat controllers like models/blocks/helpers.


When writing a controller for the new routers, you no longer have to end the classname with ‘Controller’ but you MUST extend Mage_Core_Controller_Varien_Action in some way.
Looking at the class below, you will see that we extend Mage_Customer_AccountController which extends Mage_Core_Controller_Front_Action with extends Mage_Core_Controller_Varien_Action, so we know it is a valid controller.
In our new controller, we will override a single action, loginAction(), so we can insert our custom code. You have the option of calling the parent action if needed.

First, we need to actually tell Magento that our project depends upon LKC_ModularRouters
/app/etc/modules/MyCompany_MyModule.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
MyCompany_MyModule>
            <
active>true</active>
            <
codePool>local</codePool>
            <
depends>
                <
Mage_Core />
                <
Mage_Customer />
                <
LKC_ModularRouters />
            </
depends>
        </
MyCompany_MyModule>
    </
modules>
</
config>

Next, we need to write our new controller that extends Mage_Customer_AccountController.
/app/code/local/MyCompany/MyModule/Controller/Frontend/Customer/Account.php

// This is needed since Varien used a layout that is not easily auto-loadable
require_once("Mage/Customer/controllers/AccountController.php");

class 
MyCompany_MyModule_Controller_Frontend_Customer_Account extends Mage_Customer_AccountController
{
    
public function loginAction()
    
{
        
// Your custom code here
        
        // You can call the parent action as well, if you need to
        
parent::loginAction();
    
}

    
// This is to fix an assumption in Magento that breaks translations
    
protected function _getRealModuleName()
    
{
        
return "MyCompany_MyModule";
    
}
}

Finally, instead of using URL rewriting, we are going to use the same method you use to rewrite a model/block/helper object.
/app/code/local/MyCompany/MyModule/etc/config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
MyCompany_MyModule>
            <
version>0.0.1</version>
        </
MyCompany_MyModule>
    </
modules>
    <global>
        <
controllers>
            <
Mage_Customer>
                <
rewrite>
                    <
account>MyCompany_MyModule_Controller_Frontend_Customer_Account</account>
                </
rewrite>
            </
Mage_Customer>
        </
controllers>
    </global>
</
config>


That’s all there is to it.  You now have a clean override of the core controller for customer accounts.  This can be used for core or 3rd party modules.


Note: due to a small limitation that will be resolved soon, you cannot use the short ‘frontname’ of a module when rewriting, you must use the actual module name.
Note: if the controller you are overriding is written to live under the /controllers directory (like the core controllers) then you will need to manually include the class file since the autoloader cannot find it.

 Signature 

Lokey Coding, LLC
+1-386-269-0070

 
Magento Community Magento Community
Magento Community
Magento Community
 
Meanbee
Sr. Member
 
Avatar
Total Posts:  100
Joined:  2007-09-14
Bath, UK
 

Perfect.  Good work.

 Signature 

Meanbee Internet Solutions Limited (twitter, newsletter, getsatisfaction)
UK Magento Developer
Modules: Royalmail

 
Magento Community Magento Community
Magento Community
Magento Community
 
G-rom
Jr. Member
 
Total Posts:  15
Joined:  2009-03-06
 

really really thank you, it’s working so easily !

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bastian Selonke
Jr. Member
 
Total Posts:  23
Joined:  2008-09-09
 

Unfourtunately, this doesn’t work as that well. The Zend_Loader::loadClass() function fails with an “cannot find file” exception, even if the file is definately there and the xpath to it correct. Bump.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

In response to this bug report about translations being broken, I have modified my example to include a fix to the problem.

 Signature 

Lokey Coding, LLC
+1-386-269-0070

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

@Bastian Selonke

If you have a problem using the module, please post a detail explanation of the problem and I will help you. 

If you think you have found a bug, then please post to the bugs forum to keep this thread about my example code.

 Signature 

Lokey Coding, LLC
+1-386-269-0070

 
Magento Community Magento Community
Magento Community
Magento Community
 
Elektronicanet
Jr. Member
 
Total Posts:  10
Joined:  2009-01-02
 

Thanks for this great contribution! Without this extension, I was not able to implement VAT-free deliveries inside Europe for companies. There are many topics about this.
However, there is a small typo in your code, which took a lot of time to find. In the config.xml file, your last tag is <config> and should be of course </config>. Small detail, but the difference between errors and hapiness smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

@Elektronicanet

Thanks for catching that.  I was wondering why the config file wasn’t working for a few people, now I know. =)

I’m glad the module is helpful to you. 

I will be posting 2 more sample usages in the near future.  One detailing a good way to separate your admin and frontend controllers, the other detailing how to get your admin controllers to appear under the /admin url like the core admin controllers.  So, keep a look out.

 Signature 

Lokey Coding, LLC
+1-386-269-0070

 
Magento Community Magento Community
Magento Community
Magento Community
 
G-rom
Jr. Member
 
Total Posts:  15
Joined:  2009-03-06
 

is an update schedule to make it work with 1.3 ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Elektronicanet
Jr. Member
 
Total Posts:  10
Joined:  2009-01-02
 

For me it is not working for 1.3 indeed

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Well, this isn’t really a bug forum, but I’ll respond.

I’ve been out of town since 1.3 came out.  Now that I am back, I’m looking at what is required to make this work for 1.3 and attempt to retain 1.2 compatibility.

I’ll get a new release out shortly.

 Signature 

Lokey Coding, LLC
+1-386-269-0070

 
Magento Community Magento Community
Magento Community
Magento Community
 
circa1977
Member
 
Total Posts:  65
Joined:  2008-03-01
 

It looks like I just inadvertently upgraded to 1.3.0 and am now seeing the following:

www.rackandgo.com/shop

How soon do you think you’ll have any update ready? smile

I’ve tried disabling the extension in its config.xml to no avail.

Mark

 Signature 

.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Mark J. Reeves
Principal, Technology
| @circa1977

 
Magento Community Magento Community
Magento Community
Magento Community
 
circa1977
Member
 
Total Posts:  65
Joined:  2008-03-01
 

That was an error that included a reference to LKC prior to my tinkering with config.xml files.

 Signature 

.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Mark J. Reeves
Principal, Technology
| @circa1977

 
Magento Community Magento Community
Magento Community
Magento Community
 
rteague612
Member
 
Avatar
Total Posts:  48
Joined:  2008-07-29
 

I have tentatively gotten this to work with 1.3

it seems that the method:

$this->getModuleByFrontName($module)

Now longer returns a string, but an array:

Array([0] => Mage_Customer)

or something to that effect.

By setting the $realModule variable to the first element of that array and after testing, has seemed to make this work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
J.T.
Mentor
 
Avatar
Total Posts:  1961
Joined:  2008-08-07
London-ish, UK
 

I’m trying to override /app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php but am struggling to do it the ‘usual’ way.

Is the way proposed in this thread the only currently accepted way to do it? In other words, it definitely can’t be done the way we override and extend blocks?

 Signature 

It takes two to tango, so don’t blame Magento right away if things go tits-up!

Mage Quick FAQ
Q. Installation problems with localhost/xamp/wamp/whatever and/or missing php extensions, help!
A. Get Zend Server - Community Edition is free and will make things a lot easier on you now and when deploying to production

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jason Zhang
Jr. Member
 
Total Posts:  11
Joined:  2009-05-17
 

If I want to overload a admin controller like /admin/catalog_product.
what should I do?
it seems not work if I fellow the below.
Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 3
 
 
© Copyright 2012 Magento Inc.
Privacy Policy|Terms of Service
Magento Community Count
701238 users|881 users currently online|497313 forum posts