Action Level Controller Rewriting

Time to reactivate writing #tweetorials. Today a little information on controller rewrites (there are many ways to do this in Magento).

The official best practice to rewrite controllers is to use the config/[area]/routers/[route]/args/modules/ node. This is covered elsewhere.

Or see the code that evaluates this configuration at Mage_Core_Controller_Varien_Router_Standard::collectRoutes() (ref http://alanstorm.com/magento_dispatch_standard_router)

The information I want to share is that the controller is NOT only matched by it’s file name (e.g Sales/OrderController.php)

If the requested action isn’t implemented on a controller class, Magento will continue to look through the list of modules for a match.

So don’t extend the parent class you are rewriting unless you have to. This will let other modules rewrite other methods if needed.


Layout XML Action Arguments

This #tweetorial is all about different ways to specify arguments to the layout XML’s action directive (7 tweets)

Simple two argument method call: <action method=”someThing”><p1>Val1</p1><p2>Val2</p2></action> (cont …)

… The name of the argument nodes DOES NOT MATTER, just the order counts!

To pass an array to a method: <action method=”someThing”><p1><key1>Val1</key1><key2>Val3</key2></p1></action> → array(‘key1’=>’Val1’,…)

Gotcha: you can only specify string values in XML, so <p1>false</p1> would evaluate to a true boolean. Use <p1>0</p1> instead #typecasting

Very handy: use of helper methods: <action method=”someThing”><p1 helper=”classgroup/class/someMethod”/></action> (cont…)

… or use “classgroup/someMethod” to call someMethod() on a data helper. The return value is passed to the block method as an argument.

WTF: specify JSON values (you have to use double quotes!):
<action method=”someThing” json=”p1”><p1>{“key1”:”Val1”, “key2”:42}</p1></action>