Saturday, June 15, 2013

Custom Module in Magento

Hi Freinds,

Today I am posting about custom module in magento. Hope it will be fruitful.


Creating a new module in magento

Structure of sample file:
[root]\app\code\local\{Namespace}\{Modulename}
[root]\app\code\local\{Namespace}\{Modulename}\controllers
[root]\app\code\local\{Namespace}\{Modulename}\etc
[root]\app\code\local\{Namespace}\{Modulename}\etc\config.xml
[root]\app\code\local\{Namespace}\{Modulename}\Helper
[root]\app\code\local\{Namespace}\{Modulename}\Helper\Data.php
[root]\app\code\local\{Namespace}\{Modulename}\Model
[root]\app\code\local\{Namespace}\{Modulename}\{Modulename}.php

{Namespace} is a user defined variable. 
{Modulename} this is the name of your module.


For Example I am using Donebynone as the Namespace and Helloworld as my module name

First using controllers...all controllers goes here and name this file as it suits you .Below is controller code...just for example

####################################################################################################
<?php
//IndexController is the default controller
class Donebynone_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
    //indexAction is the default Action for any controller
    function indexAction() {
        echo "indexAction";
        $helloworld = Mage::getModel("donebynone_helloworld/helloworld");
        $helloworld->helloworld("helloworld");
    }
}

####################################################################################################


Data.php is just a helper file for the Magento , I’m not exactly sure what it does but it appears to be exactly the same in every module except for its class definition.

###################################################################################################

<?php
class Donebynone_Helloworld_Helper_Data extends Mage_Core_Helper_Abstract {
}

###################################################################################################

Coming to the Model. It holds all your module.In Magento core you’ll notice that most modules contain many models.

###################################################################################################
<?php
class Donebynone_Helloworld_Model_Helloworld extends Varien_Object {
    function __construct() {
        parent::__construct();
    }
    function helloworld($arg) {
        echo "<br>Hello World! My argument is : " . $arg;
    }
}
###################################################################################################

Next is important one...config.xml  where you connect all your code up to Magento.

####################################################################################################
<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <modules>
        <Donebynone_Helloworld>
            <version>0.1.0</version>
        </Donebynone_Helloworld>
    </modules>
    <frontend>
        <routers>
            <Donebynone>    <!-- I make this match my front name but I'm not sure it matters -->
                <use>standard</use>    <!-- Use standard routing as opposed to admin.  IE: frontend vs admin -->
                <args>
                    <module>donebynone_Helloworld</module>    <!-- The module to search for controllers -->
                    <frontName>donebynone</frontName>    <!-- The first descriminator in the path.  "donebynone" in http://localhost/donebynone/ -->
                </args>
            </donebynone>
        </routers>
    </frontend>
    <global>
        <models>
            <donebynone_helloworld>    <!-- This is used when istanting your Model EG: Mage::getModel("donebynone_helloworld/hellworld") -->
                <class>donebynone_Helloworld_Model</class>    <!-- That class to use when isntanting objects of type above. -->
            </donebynone_helloworld>
        </models>
    </global>
</config>
####################################################################################################


Now Finally [root]\app\etc\modules\{Namespace}_{Modulename}.xml in our case it is Donebynone_Helloworld.xml. This file basically tells magento that there is a new module and you have to load it.

#########################################################################################################
<?xml version="1.0"?>
<config>
    <modules>
        <Donebynone_Helloworld>
            <active>true</active>
            <codePool>local</codePool>
        </Donebynone_Helloworld>
    </modules>
</config>
#########################################################################################################


Thanks no more hard work just go to  Admin->System -> Configuration -> Advanced and you should see your module enabled here.

Dont forget to flush your magento cache.

You are done!!

Also check this for reference purpose
(http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table)

No comments:

Post a Comment