Magento Override

There have been many times when all I want to do is redefine a specific action on the controller, but not all. In most cases, I just tried the entire controller, but I wonder if there is a better way? Is Magento able to simply override one action in the controller, leaving the original controller and other actions as they were?

Example:

class Mage_Core_AwesomeController extends Mage_Core_Controller_Front_Action { //has url of awesome/index public function indexAction(){ //Some Awesome code } //has url of awesome/torewrite public function torewriteAction(){ //Some Awesome code } } class Local_Core_AwesomeController extends Mage_Core_AwesomeController { //has url of awesome/torewrite public function torewriteAction(){ //Some Awesome Override code } } 

So url awesome / torewrite will go to Local_Core_AwesomeController, but the awesome / index url will be sent to Mage_Core_AwesomeController.

This example is obviously fabricated; it's just there to show what I want in theory. Therefore, please do not try to correct the example, just demonstrate the best way to redefine only the action.

I think it would also be important to note that I do not want to rewrite the URL just by redefining the action. Maybe this is not possible without rewriting the URL? It’s just that when you rewrite a URL, the tags in the layout change, and I prefer to keep them the same.

+7
source share
1 answer

In your Local/Core/etc/config.xml define your controller inside the router to override.

 <config> ... <frontend> // Use <admin> for backend routers <routers> <core> // <-- this is the router name <args> <modules> <local_core before="Mage_Core">Local_Core</local_core> </modules> </args> </core> </routers> </frontend> ... </config> 

Magento now checks Local/Core/controllers to Mage/Core/controllers in the URL path starting with core (router name). Your PHP class is already right.

This only gently hints about halfway down this page , which says:

Since Magento 1.3 you can simply add your module to an external router. Repeated calls are no longer required.

+6
source

All Articles