Tuesday, July 20, 2010

Create Your First Magento Module

7/20/2010

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>

Written by

Arvind is a web developer, programmer and blogger. He has expertise in PHP, Magento, WordPress, jQuery, JavaScript, HTML5 and CSS3. He loves to develop good looking websites with strong backend.

77 comments :

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

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

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

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

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

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

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

    ReplyDelete
  8. 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/"

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

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

    ReplyDelete
  11. I am not start from start?

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

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

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

    ReplyDelete
  15. Thanks. Good tutorial!

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

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

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

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

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

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

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

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

    thanks

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

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

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

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

    ReplyDelete
  28. "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?

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

    ReplyDelete
  30. 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?

    ReplyDelete
  31. Very useful post. I really appreciate your blog.
    Thanks

    ReplyDelete
  32. 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');
    }
    }
    ?>

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

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

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

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

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

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

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

    ReplyDelete
  40. 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?

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

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

    ReplyDelete
  43. How to control this "Company_Web" functionality?

    ReplyDelete
  44. Functionality in backed and how to view in front end?

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

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

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

    ReplyDelete
    Replies
    1. Please use models and blocks to get data on frontend.

      Delete
  47. 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?

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

    ReplyDelete
    Replies
    1. Did you download the fresh module and then tried?

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

    ReplyDelete
  50. Hi Arvind, Please reply my post Its urgent.

    Thanks
    Mahak

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

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

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

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

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

    ReplyDelete
  56. This worked at first, then it STOPPED working.

    What is the url again?

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

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

    thanks once again....friends

    ReplyDelete
  59. very helpful post for magento freshers thnks you very much

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

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

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

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

      Delete
  63. thanks.....great tutorial..

    ReplyDelete
  64. Not working in magento 1.7.0.2 after flushing the cache

    ReplyDelete
  65. Please explain it with magneto 1.7.0.2

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

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

      Delete

We would love to hear from you...

 

© 2014 Web Speaks . All rights resevered. Designed by Templateism