August 03, 2015

August 03, 2015
In this article, we are going to discuss about How to create a Helloworld custom module in Magento. Magento is an open-source content management system for e-commerce web sites. Magento employs the MySQL relational database management system, the PHP programming language, and elements of the Zend Framework. It applies the conventions of object-oriented programming and model-view-controller architecture. Magento also uses the entity–attribute–value model to store data.

Step 1: Module Declaration

Note : PCF (PHPCmsFramework)

Create app/etc/modules/PCF_HelloWorld.xml and write below code

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

Step 2: Module Configuration

2.1) Create a controller class app/code/local/PCF/HelloWorld/controllers/IndexController.php

class PCF_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
     $this->loadLayout(array('default'));
     $this->renderLayout();
    }
}

2.2) Create a Block class app/code/local/PCF/HelloWorld/Block/HelloWorld.php

class PCF_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template
{
  // necessary methods
}

2.3) create configuration xml in app/code/local/PCF/HelloWorld/etc/config.xml

<?xml version="1.0"?>
<config>
    <global>
        <modules>
                <PCF_helloworld>
                        <version>0.1.0</version>
                </PCF_helloworld>
        </modules>
    <blocks>
            <helloworld>
                <rewrite>
         <helloworld>PCF_HelloWorld_Block_HelloWorld</helloworld>
        </rewrite>
            </helloworld>
     </blocks>

        </global>
       <frontend>
                <routers>
                        <helloworld>
                                <use>standard</use>
                                <args>
                                      <module>PCF_HelloWorld</module>
                                      <frontName>helloworld</frontName>
                                </args>
                        </helloworld>
                </routers>
        <layout>
            <updates>
                <helloworld>
                      <file>helloworld.xml</file>
                </helloworld>
            </updates>
            </layout>
        </frontend>
</config>


Define Frontend Template :

1. Define page layout in app/design/frontend/PCF/default/layout/helloworld.xml

N.B: Use default instead of PCF as template location if you use default design packages. Means create file in app/design/frontend/default/default/layout/helloworld.xml

<?xml version="1.0"?>

    <layout version="0.1.0">

        <helloworld_index_index>
            <reference name="root">
                <action method="setTemplate"><template>page/1column.phtml</template></action>
            </reference>
            <reference name="content">
                <block type="helloworld/helloworld" name="hello" template="helloworld/helloworld.phtml"/>
            </reference>
        </helloworld_index_index>

    </layout>
   
2. Create template file app/design/frontend/PCF/default/template/helloworld/helloworld.phtml and write down

N.B: Use default instead of PCF as template location if you use default design packages. Means create file in app/design/frontend/default/default/template/helloworld/helloworld.phtml

Hello World ! I am a Magento Guy..

Hey, new module is ready to run and hit browser with url

http://127.0.0.1/projectname/index.php/helloworld/

and see result.

That's it……..

0 comments:

Post a Comment