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/
|
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).
Before start cooking lets fixup the name of our module is Mymodule and our package name is Mypackage .
1.Declare your shell module and it’s code pool
Create a xml file /app/etc/modules/Mymodule.xml (You can use any name, even you can use a single file to declare number of modules).
<?xml version="1.0"?>
<config>
<modules>
<Mypackage_Mymodule>
<active>true</active>
<codePool>local</codePool>
</Mypackage_Mymodule>
</modules>
</config>
2.Create the basic structure Under /app/code/core/local/ :-
Mypackage/
|–Mymodule/
| |–Block/
| |–controllers/
| |–etc/
| |–Helper/
| |–sql/
|
3.Write the front controller /controllers/IndexController.php
<?php
class Mypackage_Mymodule_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
//Do nothing fo the time being
$this->loadLayout();
$this->_initLayoutMessages('customer/session');
$this->renderLayout();
}
}
4.Create design layout for our controller action
/app/design/frontend/default/default/layout/mymodule.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
</default>
<mymodule_index_index>
<remove name="right"/>
<remove name="left"/>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="mymodule/helloworld" name="helloworld" template="mymodule/helloworld.phtml"/>
</reference>
</mymodule_index_index>
</layout>
4.Create associated block
/Block/Helloworld.php
<?php
class Mypackage_Mymodule_Block_Helloworld extends Mage_Core_Block_Template
{
public function getHelloWorldMsg()
{
return "Hello world";
}
}
5.Create Helper for this module
/Helper/Data.php
<?php
class Mypackage_Mymodule_Helper_Data extends Mage_Core_Helper_Abstract
{
}
6.Now organize every thing write the settings for this module in
/etc/config.xml
We declare the following things in out config.xml
i) Version number of the module. Magento keep track to version number in “core_resource” table which is used to run sql_setup[Details discussed in another post] .
ii) Router information for this module. It contains frontanme and access privilege (front end controller or admin ).
iii) Which layout xml to use for its design.
iv) Global path for module , blocks and helper .
v) Default connection to use for db read & write.
<?xml version="1.0"?>
<config>
<!-- i.Version Number -->
<modules>
<Mypackage_Mymodule>
<version>0.1.0</version>
</Mypackage_Mymodule>
</modules>
<!-- ii. Router information -->
<frontend>
<routers>
<mymodule>
<!-- Means front end controller -->
<use>standard</use>
<!-- Fron name "Myfrontname" declared for "Mypackage_Mymodule" -->
<args>
<module>Mypackage_Mymodule</module>
<frontName>myfrontname</frontName>
</args>
</mymodule>
</routers>
<!-- iii. Desing layout file -->
<layout>
<updates>
<mymodule>
<file>mymodule.xml</file>
</mymodule>
</updates>
</layout>
</frontend>
<global>
<!-- iv. Global path for model, blocks, helper -->
<!-- models reside under path "Mypackage/Mymodule/model/" -->
<models>
<mymodule>
<class>Mypackage_Mymodule_Model</class>
</mymodule>
</models>
<!-- blocks reside under path "Mypackage/Mymodule/Blocks/" -->
<blocks>
<mymodule>
<class>Mypackage_Mymodule_Block</class>
</mymodule>
</blocks>
<!-- Helper reside under path "Mypackage/Mymodule/Helper/" -->
<helpers>
<mymodule>
<class>Mypackage_Mymodule_Helper</class>
</mymodule>
</helpers>
<!-- v. Declaring db connections -->
<!-- Also sql setup handler declared in this portion -->
<resources>
<mymodule_write>
<connection>
<use>core_write</use>
</connection>
</mymodule_write>
<mymodule_read>
<connection>
<use>core_read</use>
</connection>
</mymodule_read>
</resources>
</global>
</config>
That’s it. Go to http://yourdomain.come/magentopath/myfrontname/ and see your first magento modules output

Nice post, thanks to share this! I will check back your site again for other Magento’s stuffs!
hi,
very nice, though should step 2 be
2.Create the basic structure Under /app/code/local/ :-
and NOT
2.Create the basic structure Under /app/code/core/local/ :-
i.e. the local folder should be under code and not core
many thanks for an excellent tutorial
Fantastic post. I’ve looked at tons of Magento how-tos, but this is by far the clearest and simplest one I’ve seen!
Thanks!
Really helpfull post Azad Rehman.I would like to add 2 more tips
1.To show the output in home page go to Magento admin > CMS > Manage Pages > Home page
add
{{block type=”mymodule/helloworld” name=”helloworld” template=”mymodule/helloworld.phtml”}}
2.At any time disable this module so that the output is not shown anywhere in the magento site
System > Configuration > Advanced
Select ‘Disable’ in the dropdown near ‘Mypackage_Mymodule’
Where do you learn how to write the config.xml and also you didn’t go over it but the system.xml?
Thanks
Hello Brain,
Thanks for commenting. 2yrs Back when I started customizing magento literally there was no good blog/books/wikis to get help or to start with. So basically I started using print_r , echo & var_dump functions to get understand how magento system works.
So.. may be my all concepts are not 100% right, but somehow it works.
You want me to write about system.xml ??? Stay tuned.. hopefully I’ll do it when I’ll get some free time.
Thanks
this is wrong.Its not working.The url returns nothing…redirects to error page.
It should work. Plz check everything again ..
Great stuff, thanks. Do you use many Magento modules? The company I work for used one that did a great deal to help to speed up page response times. It took a few months to get 100% and function to our needs but now it’s great. Saved loads on hosting by having it in a cloud vps. Thanks, Tarine.