Hi Guys, it has been ages, we have been really busy delivering some crucial projects before Christmas. Things are still busy but a quick article might save the world ????
Today we are going to talk about how to overwrite frontend controller with your custom controller. This requirement is not very common but in certain cases you need to do that. Just recently, we had to overwrite catalog frontend controller to sort out duplicate meta titles and descriptions to satisfy google. As you might know Magento is not really good at SEO front and google doesn’t like it because it produces a lot of duplicate URLs. We have sorted out most of these problems in our framework. Anyway someday we’ll talk about that in separate post, let’s crack on with overwriting frontend controller using our step by step implementation -:
Step 1 – Create a new module under your local folder, in our case under applocalScommerceCatalog.
Step 2 – Register the module in etc/modules directory either in Scommerce_All.xml or Scommerce_Catalog.xml by using the following code -:
<Scommerce_Catalog> <active>true</active> <codePool>local</codePool> </Scommerce_Catalog>
Step 3 – Create config.xml under etc directory of your module applocalScommerceCatalogetcconfig.xml by using the following code
<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Scommerce_Catalog> <version>0.0.1</version> </Scommerce_Catalog> </modules> <frontend> <routers> <catalog> <args> <modules> <Scommerce_Catalog before="Mage_Catalog"> Scommerce_Catalog </Scommerce_Catalog> </modules> </args> </catalog> </routers> </frontend> </config>
The above declaration will allow you to overwrite any of the catalog controllers like ProductController or CategoryController etc. This is great and simple way to overwrite frontend controllers and if you notice before keyword is the key which tells Magento that we want to execute our controller before Mage_Catalog.
Step 4 – Create ProductController in appcodelocalScommerceCatalogProductController.php using the following code
require_once Mage::getModuleDir('controllers', 'Mage_Catalog').DS."ProductController.php"; class Scommerce_Catalog_ProductController extends Mage_Catalog_ProductController { /** * View product action */ public function viewAction() { if ($product = $this->_initProduct()){ echo 'Product Controller has been overwritten successfully'; .......... } } }
That’s it, it is as simple as that. Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.