There has been many times when all I want to do is override a specific action on a controller but not the whole thing. In most cases I have just overrode the whole controller, but I'm wondering if there is a better way? Does Magento have a way to just override a single action in a 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 the url awesome/torewrite would go to Local_Core_AwesomeController but the url awesome/index would go to Mage_Core_AwesomeController.
This example is obviously fabricated, its merely there to show what I would want in theory. So please don't try and correct the example, just demonstrate the best way to override just an action.
I think it would also be important to note that I do not want to rewrite the url, just override the action. Maybe this is impossible without rewriting the url? Its just that when rewriting the url the tags in the layout change and I would rather keep them the same.
In order to override a controller at first, we need to create a subclass and the existing function is defined in it and from that controller now we have to select a function for overriding. For example, we can select a Carousel function for overriding.
To call your phtml file at place of core(magento's) file, you need to follow: Firstly, create a xml file with the controller name, on which page you want to override. For example : here we override checkout/cart/index controller page file.
In your Local/Core/etc/config.xml
, define your controller within the router to be overridden.
<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 will now check Local/Core/controllers
before Mage/Core/controllers
for URL paths starting with core
(the router name). Your PHP class above is already correct.
This is only gently hinted at about halfway down this page where it says:
Since Magento 1.3 you can simply add your module to the frontend router. Rewrites are not neccessary any more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With