Monday, 29 February 2016

Overidding magento controller.



Its quite different to override Magento controller than overriding Models, helpers, and blocks. Here we will see how we can override the Magento controller. Here I am taking wishlist model of Magento as an example,

1) Enable module: Enable your module configuration, at below path,

Path:  \app\etc\modules\Custom_Module.xml

<?xml version="1.0"?>

<config>

  <modules>

    <Custom_Module>

      <active>true</active>

      <codePool>local</codePool>

      <version>0.1.0</version>

    </Custom_Module>

  </modules>

</config>

2) Override the controller: you need to include require once in the controller file which you want to override, Please create controller file in below path,

Path:  \app\code\local\Custom\Module\controllers\Wishlist\ IndexController.php

<?php

require_once "Mage/Wishlist/controllers/IndexController.php";  


class Custom_Module_Wishlist_IndexController extends Mage_Wishlist_IndexController{


// your logic here


}

  
3) Module configuration: Now define the module configuration in below path,

Path: app\code\local\Custom\Module\etc\ config.php

<?xml version="1.0"?>

<config>

  <modules>

    <Custom_Module>

      <version>0.1.0</version>

    </Custom_Module>

  </modules>

  <frontend>

    <routers>

      <module>

        <use>standard</use>

          <args>

            <module>Custom_Module</module>

            <frontName>module</frontName>

          </args>

      </module>

    </routers>

  </frontend>

  <global>

                                <rewrite>       

            <custom_module_wishlist_indexcontroller>

                <from><![CDATA[#^/wishlist/index/#]]></from> <!-- Mage_Wishlist_IndexController  -->

                <to>/module/wishlist_index/</to> <!-- Custom_Module_Wishlist_IndexController  -->

            </custom_module_wishlist_indexcontroller>

                                </rewrite>

    <helpers>

      <module>

        <class>Custom_Module_Helper</class>

      </module>

    </helpers>

  </global>

  <admin>

                <routers>

                  <module>

                    <use>admin</use>

                                <args>

                                  <module>Custom_Module</module>

                                  <frontName>admin_module</frontName>

                                </args>

                  </module>

                </routers>

  </admin>

</config>


4) Setting up helper (If you wish to use):  You can also set the helper class as below,

\app\code\local\Custom\Module\Helper\ Data.php

<?php

class Custom_Module_Helper_Data extends Mage_Core_Helper_Abstract

{


// your custom logic


}

That set!!
Now we are ready to use the wishlist controller core features.

No comments:

Post a Comment