If you want to override a core controller on Magento in order to add other customActions, it is really quick and easy to do so. Here are the files that you have to take into account:
- The controller you want to override: /app/code/core/Catalog/controllers/ProductController.php
- The xml file to enable the module: /app/etc/modules/Company_Module.xml
- The controller xml file definition: /app/code/local/Company/Module/etc/config.xml
- The overridden controller: /app/code/local/Company/Module/controllers/ProductController.php
First of all, you have to enable the module on the /app/etc/modules directory. This file would be Company_Module.xml and inside that:
<?xml version="1.0"?> <config> <modules> <Company_Module> <active>true</active> <codePool>local</codePool> </Company_Module> </modules> </config>
Now it’s time to create the config.xml file that will override the core controller:
<?xml version="1.0"?> <config> <frontend> <routers> <catalog> <args> <modules> <Company_Module before="Mage_Catalog">Company_Module</Company_Module> </modules> </args> </catalog> </routers> </frontend> </config>
If your controller is placed in a folder inside the folder controller, you have to change the reference. For example, if your module is in the folder controllers/Folder, then the config.xml should look like this:
<?xml version="1.0"?> <config> <frontend> <routers> <catalog> <args> <modules> <Company_Module before="Mage_Catalog">Company_Module_Folder</Company_Module> </modules> </args> </catalog> </routers> </frontend> </config>
Finally, we can create the class that will add/change actions to the core controller:
class Smile_Catalog_ProductController extends Mage_Core_Controller_Front_Action { public function customAction() { /* .... */ } }
Now you should check if it your custom action works. You can call that function, in this case, using the route: http://yourweb.com/catalog/product/customAction/
- There are also other ways of overriding magento core modules, for example when you want to override a magento core block class.
- If you want to override a magento admin controller, take a look at this post.
0 Comments