PDA

View Full Version : How to overload a controller



AndrewJohn26
05-16-2011, 07:01 AM
How to overload a controller in magento .?

mtthwsmith8
06-04-2011, 05:20 AM
In this example we’re overloading Mage_Checkout_CartController::indexAction().
1. Create your module folders and files
1. Magento/app/code/local/MyNameSpace/MyModule/etc/config.xml
2. Magento/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
3. Magento/app/etc/modules/MyNameSpace_All.xml
2. Edit /etc/config.xml
Go to Magento/app/code/local/MyNameSpace/MyModule/etc/config.xml and paste the following xml into it (comments I’m not a 100% sure about are ending with “(?)”):
1. <?xml version="1.0"?>
2. <config>
3. ***** ***** <modules>
4. ***** ***** ***** ***** <MyNameSpace_MyModule>
5. ***** ***** ***** ***** ***** ***** <version>0.1.0</version>
6. ***** ***** ***** ***** </MyNameSpace_MyModule>
7. ***** ***** </modules>
8. ***** ***** <global>
9. ***** ***** ***** ***** <!-- This rewrite rule could be added to the database instead -->
10. ***** ***** ***** ***** <rewrite>
11. ***** ***** ***** ***** ***** ***** <!-- This is an identifier for your rewrite that should be unique -->
12. ***** ***** ***** ***** ***** ***** <!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
13. ***** ***** ***** ***** ***** ***** <mynamespace_mymodule_checkout_cart>
14. ***** ***** ***** ***** ***** ***** ***** ***** <from><![CDATA[#^/checkout/cart/#]]></from>
15. ***** ***** ***** ***** ***** ***** ***** ***** <!--
16. ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** - mymodule matches the router frontname below
17. ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** - checkout_cart matches the path to your controller
18. ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
19. ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** Considering the router below, "/mymodule/checkout_cart/" will be
20. ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** "translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
21. ***** ***** ***** ***** ***** ***** ***** ***** -->
22. ***** ***** ***** ***** ***** ***** ***** ***** <to>/mymodule/checkout_cart/</to>
23. ***** ***** ***** ***** ***** ***** </mynamespace_mymodule_checkout_cart>
24. ***** ***** ***** ***** </rewrite>
25. ***** ***** </global>
26. ***** ***** <!--
27. ***** ***** If you want to overload an admin-controller this tag should be <admin> instead,
28. ***** ***** or <adminhtml> if youre overloading such stuff (?)
29. ***** ***** -->
30. ***** ***** <frontend>
31. ***** ***** ***** ***** <routers>
32. ***** ***** ***** ***** ***** ***** <mynamespace_mymodule>
33. ***** ***** ***** ***** ***** ***** ***** ***** <!-- should be set to "admin" when overloading admin stuff (?) -->
34. ***** ***** ***** ***** ***** ***** ***** ***** <use>standard</use>
35. ***** ***** ***** ***** ***** ***** ***** ***** <args>
36. ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** <module>MyNameSpace_MyModule</module>
37. ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** <!-- This is used when "catching" the rewrite above -->
38. ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** <frontName>mymodule</frontName>
39. ***** ***** ***** ***** ***** ***** ***** ***** </args>
40. ***** ***** ***** ***** ***** ***** </mynamespace_mymodule>
41. ***** ***** ***** ***** </routers>
42. ***** ***** </frontend>
43. </config>
3. Edit /controllers/Checkout/CartController.php
Paste the following php code into Magento/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php (the only change we’re doing to the indexAction() is adding an error_log() message):
1. <?php
2. # Controllers are not autoloaded so we will have to do it manually:
3. require_once 'Mage/Checkout/controllers/CartController.php';
4. class MyNameSpace_MyModule_Checkout_CartController extends Mage_Checkout_CartController
5. {
6. ***** ***** # Overloaded indexAction
7. ***** ***** public function indexAction()
8. ***** ***** {
9. ***** ***** ***** ***** # Just to make sure
10. ***** ***** ***** ***** error_log('Yes, I did it!');
11. ***** ***** ***** ***** parent::indexAction();
12. ***** ***** }
13. }