Create Your First Magento Module

Magento module
Modules are the core functioning unit in a Magento system. Here I am giving an overview of how to write a “Hello World” module in magento. Simple basics before start you cooking :-)
At the time of instantiating, magento system reads all the Xmls inside the folder /app/etc/modules/ to get the list of active modules and their corresponding code repository . So very first step of creating a magento module is to declare the module and its core repository(the repository of the module, in magento it’s calles codepool ).
If you open /app/code/ you will see 3 coodpool :- core, community and local.
app/
|–code/
| |—community/
| |—core/
| |—local/
|
Download Magento Module

The “core” cood pool is dedicated for magento’s all system modules. The “community” codepool is for all modules developed by magento’s partners and local is the codepool dedicated for you as a developer to write your own customized code.
In Magentos’ all modules are organized under a package. This package is nothing but a simple folder under codepool containing different modules. For example all core modules of magento system is kept unde package “Mage” (open the folder /app/code/code/ , you see the folder “Mage” under which all system module resides).

Namespace: Company
Modulename : Web

Step 1: Declare your shell module and it’s code pool
Create an xml file /app/etc/modules/Company_Web.xml (You can use any name, even you can use a single file to declare number of modules).
<?xml version="1.0"?>
<config>
    <modules>
        <Company_Web>
            <active>true</active>
            <codePool>local</codePool>
        </Company_Web>
    </modules>
</config> 

Step 2:
Create the basic directory structure under /app/code/local/ :-
Company/
|–Web/
| |–Block/
| |–controllers/
| |–etc/
| |–Helper/
| |–sql/
|

Step 3:
Write the front controller in app\code\local\Company\Web\controllers\IndexController.php
<?php
class Company_Web_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
  $this->loadLayout();     
  $this->renderLayout();
    }
}

Step 4: Finally create the template file of your module app\design\frontend\default\default\template\web\web.phtml
<?php
 echo "Welcome to your custom module....";
?>

Step 5: Add the layout.xml as app\design\frontend\default\default\layout\web.xml
<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
    </default>
    <web_index_index>
        <reference name="content">
            <block type="web/web" name="web" template="web/web.phtml" />
        </reference>
    </web_index_index>
</layout> 

Step 6: Now write the helper class app\code\local\Company\Web\Helper\Data.php
<?php

class Company_Web_Helper_Data extends Mage_Core_Helper_Abstract
{

}


Step 7:
Create the config file as app\code\local\Company\Web\etc\config.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Company_Web>
            <version>0.1.0</version>
        </Company_Web>
    </modules>
    <frontend>
        <routers>
            <web>
                <use>standard</use>
                <args>
                    <module>Company_Web</module>
                    <frontName>web</frontName>
                </args>
            </web>
        </routers>
        <layout>
            <updates>
                <web>
                    <file>web.xml</file>
                </web>
            </updates>
        </layout>
    </frontend>
    <global>
        <models>
            <web>
                <class>Company_Web_Model</class>
                <resourceModel>web_mysql4</resourceModel>
            </web>
            <web_mysql4>
                <class>Company_Web_Model_Mysql4</class>
                <entities>
                    <web>
                        <table>web</table>
                    </web>
                </entities>
            </web_mysql4>
        </models>
        <resources>
            <web_setup>
                <setup>
                    <module>Company_Web</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </web_setup>
            <web_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </web_write>
            <web_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </web_read>
        </resources>
        <blocks>
            <web>
                <class>Company_Web_Block</class>
            </web>
        </blocks>
        <helpers>
            <web>
                <class>Company_Web_Helper</class>
            </web>
        </helpers>
    </global>
</config>

I'm reading: Create Your First Magento ModuleAdd To Yahoo Add To Del.icio.us Stumble This Fav This With Technorati Digg This Add To Facebook Add To Reddit Add To Yahoo

33 comments:

PixelCrayons
said...

Magento is a leading eCommerce platform. I think this tutorial will really help the beginners to start with Magento module.

Anonymous said...

Very good post! Greet thank you.
And you showed how this example can be verified - that is more important as for me.

arvind07
said...

Thanx...

Anonymous said...

Holy Crap!
That is the most work for a 'hello world' that i have ever seen

Maruschka
said...

Magento is a very good CMS which is providing a more features and facility.So,this blog is giving a good information on the Magento....thanks...
-magento mobile

Anonymous said...

Hello,
I followed your code for creating a new module and also create according to your instruction and structure but still i am getting 404 not found error. I am fresher in magent. Can u help me !

Thanks,
kamal

Anonymous said...

I followed all above steps and successfully got one more tab on admin panel menu but when clicking the Add New only a blank page is displayed. What to do?

Please Help as early as possible...

Thanx in Advance

arvind07
said...

Hi, I will, upload the zip file of the module by this evening. Please be patient.

SeƱor Gonzalez
said...

Tere is a typo in step 2:
Step 2:
Create the basic directory structure under /app/code/core/local/ :-
the correct path should be "/app/code/local/"

arvind07
said...

Thanks for the notice Senor. The typo has been corrected.

Anonymous said...

hi arvind. i cnt see any of the input types in the admin panel tab.
im using 1.5 ver of magento. please try to help it out thanks

le
said...

help me Magento please!

le
said...

I am not start from start?

vishal sanwar said...

plz give short description also ,it's helpful for understanding the code.
THANX

Anonymous said...

what is the link of example, i try this
http://127.0.0.1/dev/index.php/Company i get 404 error

Rahul
said...

Thanks a lot man.At first it didn’t workout but after clearing the cache it works fine. Great post. keep it up…..

Anonymous said...

Thanks. Good tutorial!

Tim said...

How can I show this module in Magento?

I am curious how it can be done in the template and as block in a CMS page.

Anonymous said...

Good day!

took your zip-file, unzipped it in / app

but the module is not installed correctly
have a position in System -> Advenced -> Disable Modules Output (Enable)

admin menu in no position Company

how can there be problems?


Thank you!


P.S. magemto 1.6.0

Anonymous said...

what url do i use to view the hello world module?

nikul
said...

how to call module in url?
please give me path of url

nikul
said...

how to call module in url
pleas e give me path of url

arvind07
said...

Hi Nikul,
You can call it as : http://127.0.0.1/projectname/index.php/web/

Pierre FAY
said...

if you don't khnow what to do after this tutorial you can check here to have more advanced tutorial:

http://www.about-magento.com/magento-developper-guide-howto-tutorial-5

i hope it'll help

Pierre FAY

Anonymous said...

hi arvind ! you have posted very good tutorial and is beneficial specially for the beginners.

thanks

arvind07
said...

Thanks:)

amit said...

hi arvind,
I have called module at front by this url http://127.0.0.1/projectname/index.php/web/
but can I control content of this module from admin.

arvind07
said...

Hi Amit,
This module shows the frontend functionality only. To know about backend functionality read this tutorial.

magento
said...

This blog Is very informative, I found it helpful..

Anonymous said...

I just follewed the steps but It still getting blank page. it means that my phtml page in \default\default\layout\web.xml is not called. Anyone can help me pls.. Thanks

Matt V
said...

"I just follewed the steps but It still getting blank page. it means that my phtml page in \default\default\layout\web.xml is not called. Anyone can help me pls.. Thanks"

I am getting the same problem. Can anyone help?

Magento Development India
said...

Really great post. This post will help to all Magento Developers.
Thanks for sharing.

Web Development India
said...

Very great tutorial about Magento Module but i think it is incomplete. As This module shows the frontend functionality only. And what about backend functionality?

Post a Comment