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.

Simple debugging with Magento (Using default and custom log files)


Several time developers need debugging to solve issues, in this post we will see how we can debug easily in Magento.

1) Default logs :  

If you want to check some data to confirm the values in it. You can simply use below code,

Mage::log("Check my data");

Now question is, where I can find the result of above code?

Simple: You can get the result at var/log/ folder and files will be,

 - exception.log
 - system.log

2) Custom log files creation:

Moving ahead you can also write your own custom logs for more advance debugging as below,

Use below statement,

Mage::log("Check your data", null, yourlogfile.log);

Now same question after declaration of above code where I can find the results?

Simple, you can find the result in directory var/log/

Now, I hope it will help you to debug your code or any Magento features. 




Getting current module details in Magento

1)  Getting current module name :

=> Mage::app()->getRequest()->getModuleName();

2)  Getting current controller name :

=> Mage::app()->getRequest()->getControllerName();

Magento Useful paths

Getting Magento path :

1) Mage::getBaseDir();
 Return output : /var/www/html/yourwebsite

2) Mage::getBaseDir('app')
 Return output : /var/www/html/yourwebsite/app

3) Mage::getBaseDir('media')
 Return output : /var/www/html/yourwebsite/media

4) Mage::getBaseUrl('js')
Return output : http://yourwebsite.com/js/

5) Mage::getBaseUrl('skin')
Return output : http://yourwebsite.com/skin/