Magento – the best way to override a controller class

controllersmagento-1.7overrides

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.

Best Answer

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 ;)

Related Topic