1

As I know there are two ways to override a controller class.

  1. Using from/to tags in config.xml

                        Considering the router below, "/mymodule/checkout_cart/" will be
                        "translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
                    -->
                    <to>/mymodule/checkout_cart/</to>
                </mynamespace_mymodule_checkout_cart>
            </rewrite>
        </global>
    
  2. Using 'before' keyword in config.xml

     <config>
            <admin>
                <routers>
                    <adminhtml>
                        <args>
                            <modules>
                                <mynamespace_mymodule before="Mage_Adminhtml">Mynamespace_Mymodule_Adminhtml</mynamespace_mymodule >
                            </modules>
                        </args>
                    </adminhtml>
                </routers>
            </admin>
        </config>
    

Can anyone explain what is the best way to override a controller or the above methodologies are using for different purposes of overriding. Any suggestions will be appreciated.

Thank you.

8
  • I usually use the second method but it's just a personal reference. In my opinion it's easier/ cleaner Commented Sep 30, 2013 at 9:20
  • Yes. I'm also using the second way. But there should be a reason to have two methodologies for the same task right?
    – Sukeshini
    Commented Sep 30, 2013 at 9:25
  • added my reply as answer 'cause it was getting a bit too long for a comment Commented Sep 30, 2013 at 9:34
  • Did you manage to resolve your question? Commented Oct 1, 2013 at 10:44
  • Not yet. I couldn't find a exact solution for this.
    – Sukeshini
    Commented Oct 1, 2013 at 10:45

2 Answers 2

2

The from/to rewrite is, in my opinion, there to make your extensions SEO friendly.

For example a 'brands' extension would rewrite from the default domain.com/brandsextension/index/list to a nice domain.com/brands/. Using it to reroute Magento core extensions to your own controller works fine but if it ever changes URL's from for example checkout/cart/ to checkout/shoppingcart it would break your extensions functionality.

I would suggest going with the second option since it is, as far as I know, more robust. But this is all opinion based so feel free to correct me ;)

4
  • I searched a lot. But nobody has mentioned why there are two functionalities to override magento controller. Some has mentioned whatever the method you like you can use. Some has mentioned if first one didn't work then use the second way. But these things cannot accept without any proof. Please be kind enough to provide proper explanation on this. Thanks all
    – Sukeshini
    Commented Oct 1, 2013 at 17:10
  • I'm not sure but this methods came with different magento versions or same version ?
    – Sukeshini
    Commented Oct 1, 2013 at 17:12
  • 1
    I've checked a few extensions out there and most use the second option. Using the first option to overwrite the controller really is a hack because it's supposed to be used for URL rewriting and not overwriting a controller. It so happens to be that you can use it for that but I would advice against is because of reasons previously stated Commented Oct 2, 2013 at 8:29
  • Yes by using the first way we can rewrite the URI as well.
    – Sukeshini
    Commented Oct 2, 2013 at 8:40
1

I have possibly found the reason behind 2 type of the controller overwrite procedure.

  • When you want to overwrite a controller and also want to change the default URL the use 1st procedure.

  • When you do not want to change the URL and only want to overwrite the controller then user the 2nd procedure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.