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,
Mage_Checkoutmodule is called. If we want to override this by
myaddActionas 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 create
myaddActionmethod defined within it. The way to do this is better explained in this wiki. Then the configuration required in
My_Checkoutmodule 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>Here we also need configuration to define module front name for
My_Checkoutmodule as below:
<frontend> <!-- It will be admin for overriding admin controller --> <routers> <mycheckout> <use>admin</use> <args> <module>My_Checkout</module> <frontName>mycheckout</frontName> </args> </mycheckout> </routers> </frontend> <!-- It will be admin for overriding admin controller -->Note: The above configuration examples only display portions of
config.xmlfile. Please do not consider it as a complete configuration.
Simple isn't it? Now let's understand how it works. The work flow of this rewrite process is little bit tricky.
- When an
checkout/cart/addaction is going to be dispatched, first it is passed through rewrite process.
- Rewrite process tries to find
global/routers/checkout/rewrite/cartnode is found in configuration, where
checkoutis front name of
- 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.
- Now, under this node, it tries to find whether
false. By default value of
override_actionsis true. So if it is not added in configuration, it is considered as true.
true, it overrides all actions of
Mage_Checkout_CartControllerwith same actions of
My_Checkout_MycartControlleras defined by
mycheckout/mycart. For example, if we have defined
My_Checkout_MycartController, then it automatically overrides both
Mage_Checkout_CartController. In short using
override_actionsnode we can override whole controller instead of individual actions.
actions/addnode is defined where,
override_actionsnode value is not considered and overrides action by value of
actions/add/tonode which is
myaddAction. So we can also override individual actions by this type of configuration.