Magento

Overriding Controllers and Actions in Magento

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>

  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 whether 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

 

Tags :