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 appcodelocalCompanyWebcontrollersIndexController.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 appdesignfrontenddefaultdefaulttemplatewebweb.phtml

<?php
echo "Welcome to your custom module....";
?>

Step 5: Add the layout.xml as appdesignfrontenddefaultdefaultlayoutweb.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 appcodelocalCompanyWebHelperData.php

<?php

class Company_Web_Helper_Data extends Mage_Core_Helper_Abstract
{

}
Step 7:
Create the config file as appcodelocalCompanyWebetcconfig.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>

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

84 thoughts on “Create Your First Magento Module

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

  2. 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

  3. 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

  4. 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

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

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

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

  8. 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

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

    thanks

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

  11. “I just follewed the steps but It still getting blank page. it means that my phtml page in defaultdefaultlayoutweb.xml is not called. Anyone can help me pls.. Thanks”

    I am getting the same problem. Can anyone help?

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

    I am getting the same problem. Can anyone help?

    add one more step..
    appcodelocalCompanyWebblockweb.php

    setTemplate(‘web/web.phtml’);
    }
    }
    ?>

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

    Step 3:
    Write the front controller in appcodelocalCompanyWebcontrollersIndexController.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?

  14. 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.

  15. 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

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

  17. 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 .

  18. 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.

  19. 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..

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

  21. 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

    1. 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.

  22. I follow above steps but i get this error

    “Fatal error: Class ‘Company_Web_Helper_Data’ not found in C:xampphtdocsmagentappMage.php on line 547”

Comments are closed.