Home Magento eCommerce

Overriding Controllers and Actions in Magento

28 June 2009 7 Comments
In my previous post, I had explained a way to add extra controllers to existing route. Here I am going to explain another method which is used for overriding controllers and actions. Like my each post on tips & tricks, I am going to first explain where the trick is applicable! I am considering that readers of this post are aware of MVC architecture in Magento.

Let me explain the difference between the both tricks. The trick explained in previous post is used when we need additional URLs in same route i.e. additional controllers and actions. I have already explained an example there. Here we need to change the behavior of existing controller and action. For example, when a customer adds a product to shopping cart, addAction of CartController of Mage_Checkout module is called. If we want to override this by My_Checkout, MycartController, myaddAction as similarly as we can override Models and Blocks in our own modules. So the the trick in previous post is used for overloading while the trick here is used for overriding.

Of course, this too can be done just by simple XML configuration (apart from creating custom controllers and actions) but without using route rewriting approach! Let”s take a same example above where we want to override action for checkout/cart/add. First this is to create My_Checkout module with MycartController class and myaddAction method defined within it. The way to do this is better explained Then the configuration required in etc/config.xml of My_Checkout module is like below:

<global>
   <routers>
       <checkout> <!-- Mage_Checkout module -->
           <rewrite>
               <cart> <!-- CartController -->
                   <to>mycheckout/mycart</to> <!-- My_Checkout module, MycartController -->
                   <override_actions>true</override_actions>
                   <actions>
                       <add> <!-- addAction -->
                           <to>mycheckout/mycart/myadd<to> <!-- My_Checkout/MycartController/myaddAction -->
                       </add>
                   </actions>
               <cart>
           </rewrite>
       </checkout>
   </routers>
</global>

Simple isn”t it? Now let”s understand how it works. The workflow of this rewrite process is little bit tricky.

  1. When an checkout/cart/add action is going to be dispatched, first it is passed through rewrite process.
  2. Rewrite process tries to find global/routers/checkout/rewrite/cart node is found in configuration, where checkout is front name of Mage_Checkout module and cart indicates Mage_Checkout_CartController.
  3. If this node is not found, rewrite process is not be continued and returned to dispatch process. So the action is executed normally. Otherwise, rewrite process is continued.
  4. Now, under this node, it tries to find weather override_actions node is true or false. By default value of override_actions is true. So if it is not added in configuration, it is considered as true.
  5. If override_actions is true, it overrides all actions of Mage_Checkout_CartController with same actions of My_Checkout_MycartController as defined by to node value mycheckout/mycart. For example, if we have defined addAction and indexAction methods inside My_Checkout_MycartController, then it automatically overrides both addAction and indexAction of Mage_Checkout_CartController. In short using to node and override_actions node we can override whole controller instead of individual actions.
  6. If actions/add node is defined where, add indicates addAction of Mage_Checkout_CartController, then override_actions node value is not considered and overrides action by value of actions/add/to node which is mycheckout/mycart/myadd i.e. My_Checkout module, My_Checkout_Mycontroller and myaddAction. So we can also override individual actions by this type of configuration.

Sounds good again? Try it too…

From web-magician.blogspot.com

Related posts:

  1. Secret feature of Magento 1.3: Sharing same route name for different modules!
  2. Move mini-cart in the sidebar to the header (or anywhere) in Magento

7 Comments »

  • weight loss pills said:

    What an interesting blog, bookmarked, wiil come back here miauccyzynsi

  • Ahsan said:

    Really nice explanations :)
    Thanks!
    Haven’t tried it yet but going to try your way ….

  • sukumar said:

    Thanks for post about magetno ecommerce . this very nice article . this article will use more magento vendors.Magento eCommerce

  • magento tutorial said:

    i need this tutorial,thank you so much

  • Abdul Jamal said:

    superb post

    many thanks

  • Rahul said:

    Great tutorial. Short, understandable and useful.
    Thanks For sharing us.

  • Merk said:

    Great tutorial. easily understandable and useful. Thx.