Create Your First Magento Module

79 comments
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>

79 comments

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

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

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

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

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

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

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

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/"

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

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

help me Magento please!

I am not start from start?

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

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

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

Thanks. Good tutorial!

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.

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

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

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

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

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

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

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

thanks

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.

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

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

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 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?

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

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?

Very useful post. I really appreciate your blog.
Thanks

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?


add one more step..
app\code\local\Company\Web\block\web.php

setTemplate('web/web.phtml');
}
}
?>

setTemplate('web/web.phtml');
}
}
?>

This comment has been removed by the author.
This comment has been removed by the author.
This comment has been removed by the author.
This comment has been removed by the author.
This comment has been removed by the author.
This comment has been removed by the author.

Finally got this to work from your source files. Can you please explain what each step does.

Step 3:
Write the front controller in app\code\local\Company\Web\controllers\IndexController.php

Something like this... what does the indexcontroller do? Thank you this tutorial helped me understand magento a little bit. Do you have any tutorials to create simple back end things like text areas or inputs?

I have follow all the steps and recheck many times but module is not working...any one can help me ?

It is not showing in the backend. Please help me to show, can any one help me?

How to control this "Company_Web" functionality?

Functionality in backed and how to view in front end?

This is really interesting and very nicely documented articles. I hope this will certainly helps all the Magento naive to start with this.

Thanks for sharing.

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

Hello This is really good tutorial and helped me a lot but I want to display the data from my backend to my frontend. In Web.phtml i have called the function as $web = $this->getWeb();
print_r ($web);

But not able to see the data in the frontend Web page. If u can help me with this it will be great.

Thanks

Please use models and blocks to get data on frontend.

i have installed magento 1.7.0 and i want to create new module in magento , i follow all steps as mention above but i can't see how module is working can anyone help me?

I think you should clear the cache.

i have clear cache but still i can't get output is there any other way?
if u help me then it will be great .

Did you download the fresh module and then tried?

are there any small size extensions available for initial learning purpose? if yes then please send me links it will be very helpful for me ,
thanks in advance.

Hi Arvind, Please reply my post Its urgent.

Thanks
Mahak

Great, thank for this tip.
Magento is too difficult.
I also find out an other basic tip at http://xme.im/develop-hello-world-magento-extension and http://inchoo.net/ecommerce/magento/programming-magento/magento-hello-world-module-extension/ I think that usefull for newbie. Thank to all.

Great thanks for giving such good links for magneto starter .
Thanks you all,

Hey,
Very informative artical Thanks For Sharing ,This artical is very useful for those who are new in magento platform ,
thanks for sharing and keep updating..

but not working......:(

Thank you so much... I was looking for this module and it worked perfectly fine..
Keep it up !!!!

Thanks a lot for this intro tutorial. Now I really understand how Magento works with Modules.

This worked at first, then it STOPPED working.

What is the url again?

Can someone help me set up routing with multi store setup?

thanks guys its very helpful create module's for new magento friends,

thanks once again....friends

very helpful post for magento freshers thnks you very much

works great....
more help on code would have been great
tnx :)

thanks a lot... it is a great.... but please anyone tell me how to show back end items in the front end. thanks.

thanks... this is a great tutorial... but please tell me how to display added items in the frontend.
tnx:)

You can call it as : http://127.0.0.1/magento/index.php/web/

thanks.....great tutorial..

Not working in magento 1.7.0.2 after flushing the cache

Please explain it with magneto 1.7.0.2

Hi,
I have followed your steps above and created two database tables : chefdetail and chefproduct under the web module.
I have inserted data inside these tables. Now I would like to read this data and display it on the frontend using my chefdetail.phtml and chefproduct.phtml. However it doesn't display anything.
The Block chefdetail looks liked:
class TruffleStreet_Web_Block_ChefDetail extends Mage_Core_Block_Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}

public function getChefDetail()
{
if (!$this->hasData('chefdetail')) {
$this->setData('chefdetail', Mage::registry('chefdetail'));
}
return $this->getData('chefdetail');

}
}
This is not returning anything.
In my chefdetail.phtml I am calling it as follows:
$_chefblockData = $this->getLayout()->createBlock('web/chefdetail')->getChefDetail();

Please advice how to fix this issue.
How do I load all the data in the chefdetail table into this block?

Thanks,
Neet

You have to create separate entities in config.xml for two tables. Also all the blocks and models will be different. Please follow the same steps for second table also.

I follow above steps but i get this error

"Fatal error: Class 'Company_Web_Helper_Data' not found in C:\xampp\htdocs\magent\app\Mage.php on line 547"

Hire PHP developer who knows how to develop good web application in every platform.

We would love to hear from you...

back to top