Magento Hello world module (extension)

Magento Hello world module (extension)

Previously I already wrote how to build custom module (extension) in Magento. Here is a new, refreshed article on that subject.

We will start by creating a simple “Hello world” module. However, you will soon see that simple takes a new meaning with Magento. Creating a bare bone module requires at least two files in Magento. For your module to work you need /app/etc/modules/MyCompany_MyModule.xml, app/code/local/MyCompany/MyModule/etc/config.xml. But, barebone module will not give you a “Hello developer” 🙂 -So, we need to add few more files to the game.

file 1:
/app/etc/modules/Inchoo_HelloDeveloper.xml

content of file 1:

<?xml version="1.0"?>
 
<config>
<modules>
<Inchoo_HelloDeveloper>
<active>true</active>
<codePool>local</codePool>
</Inchoo_HelloDeveloper>
</modules>
</config>

Each module starts and ends with “config” element. Most simple modules have merely one sub-element under the “config” called “modules”, It holds the definition of individual modules. In our case we have only one module declared under the “modules” element, called Inchoo_HelloDeveloper. Not the “two part” name the module has. It is a practise (and necessity) to call your modules by MyCompanyName plus TheModuleItSelf. There are three places in the system where modules can reside: /app/code/community/, /app/code/core/, /app/code/local/ folders. Take a look at file 1 content for a moment. Notice the “codePool” element? Its value is currently set to “local”, meaning our module config instructed the module to look for module files in /app/code/local/ folder. Whereas “Inchoo_HelloDeveloper” element is a mapping to /app/code/local/Inchoo/HelloDeveloper/ location.

file 2: app/code/local/Inchoo/HelloDeveloper/etc/config.xml

content of file 2 (partial):

<?xml version="1.0"?>
 
<config>
<modules>
<Inchoo_HelloDeveloper>
<version>0.1.0</version>
</Inchoo_HelloDeveloper>
</modules>
</config>

Content of file 2 is the base module config file. With the above two files you should be able to see your module listed under Magento Admin > System Configuration > Advanced > Advanced. For now we have our module “working”, but we do not see no direct results of its work, no output. So we need to add a bit more to it. First we will expand config.xml file giving it some extra parametars to “turn on” controllers so that I can call them from frontend.

content of file 2 (final):

<?xml version="1.0"?>
 
<config>
<modules>
<Inchoo_HelloDeveloper>
<version>0.1.0</version>
</Inchoo_HelloDeveloper>
</modules>
<frontend>
<routers>
<Inchoo_HelloDeveloper_SomeFreeRouterName1>
<use>standard</use>
<args>
<module>Inchoo_HelloDeveloper</module>
<frontName>inchoo-hellodeveloper</frontName>
</args>
</Inchoo_HelloDeveloper_SomeFreeRouterName1>
</routers>
</frontend>
</config>

Notice the additions we added to final file!? We needed a way to tell Magento to “use” controllers of a module. Controllers are placed in app/code/local/Inchoo/HelloDeveloper/controllers/ folder.

And to wrap this one up, we will add one more file, the cntroller we would like to call from the frontend. As you might notice there is an element called “frontend” within wich we define routers. Routers are defined in sub-element “routers” with a few additional parametars. Notice the element “Inchoo_HelloDeveloper_SomeFreeRouterName1”? This is merely a router name. It can be freely assigned. Parametar “use” can have value “admin” or “standard”, while element “args” mainly holds two additional parametars, “module” and “frontName”. Element (param) “frontName” is most interestig since its value defines the url on which the controller will be accessible. In our example, if I were to open url http://shop.local/index.php/inchoo-hellodeveloper or http://shop.local/inchoo-hellodeveloper I would see “Hello developer…”. And if I were to open http://shop.local/index.php/inchoo-hellodeveloper/index/sayHello I would see ‘Hello one more time…’;

file 3: app/code/local/Inchoo/HelloDeveloper/controllers/IndexController.php

content of file 3 (final):

<?php
 
class Inchoo_HelloDeveloper_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
echo 'Hello developer...';
}
 
public function sayHelloAction()
{
echo 'Hello one more time...';
}
}
?>

Altough extremely simple, file 3 shows us one important thing: Naming connvention. Note the name of the class. Your moudle classes should keep names in form of MyCompany_MyModule_FileName, or in case of block and modules: MyCompany_MyModule_Block_FileName or MyCompany_MyModule_Module_FileName.

Thats it for “simple hello world” module.

You made it all the way down here so you must have enjoyed this post! You may also like:

ERP and eCommerce platform integration Maja Kokolari
Maja Kokolari, | 9

ERP and eCommerce platform integration

Magento 2 Product Quick View Tomas Novoselic
, | 6

Magento 2 Product Quick View

Create configuration for your Magento extension Mladen Lotar
Mladen Lotar, | 29

Create configuration for your Magento extension

48 comments

  1. I never knew that needed a way to tell Magento to “use” controllers of a module. I can’t really imagine how they connect it. Thank you for the explanation about extension module.

  2. All perfect!! But one thing you should explain here when we disable the custom module in System->Configuration->Advanced, why it is appearing same, means why is this still displaying??

  3. Thank you, this code works perfect.
    Please help, how can i add header and footer code in this which is already fixed?

  4. I am using the same code but my error is 403 i.e Access forbidden!,
    so how can i resolve this

  5. Those who getting 404 error please make sure about the spell of directory name “controllers”. Due to wrong spell you can not do this.

  6. how can i load a view on this module ?
    EXEMPLE:
    local.xml file

    has any way to do this ? if has how ? how can i find handle name to try that ?

    Thank and sorry for this question if are much dumbed, i am starting to learn about magento today.

  7. Very narrative and useful tutorial. Every beginner should follow this tutorial to start developing Magento modules.
    So many many thanks to author from the deepest part of my heart.

  8. i am using magento 1.9.2.1 and i try to create “hello world” module. i follow all instruction given above but it gives 404 error. please give me correct code for magento 1.9.2.1 verson

  9. Hi, I am using Magento 1.9.2.1 with WAMP configuration (PHP 5.5.12). I tried the same steps, but I am facing the 404 error. Please tell me if I should make any changes for this.

    1. Hi buddy , THIS IS old tutorial , so it will not work in magento 1.9.2.1 as it has new structure.

  10. I think the author missed a point.

    Disable the cache. To Disable the cache Go to Magento Admin Panel > System > Cache Management > Select all cache type from left side checkboxes > Select Action: disable from right top drop down > click Submit

  11. I create two hellodevelopers file 1) Inchoo_HelloDevelopers and 2) Inchoo_2_HelloDevelopers and give frontname for 1) inchoo-hellodevelopers and 2 ) inchoo_2-hellodevelopers.

    Inchoo_HelloDevelopers were working perfectly but Inchoo_2_helloDevelopers is not wprking i am getting 404 error

  12. Hi, This tutorial helps me lot. Thank you very very much. Can you Guide to print the same helloworld inside magento layout.

  13. I have followed the steps mentioned above but it gives
    Whoops, our bad…
    404 error.

    Even I have cleared the cache but problem is still same..could you help me out with this error.

  14. Thank you for the above code,i have one query regarding block and layout after adding block and layout code is not working .would you please help me .

  15. I really love your tutorial. After a long struggling i created a module. Keep it up.

  16. Hello friend , it is nice article ,, i can understand , how to make module , but i have one confusion , how can i display this module in frontend?

  17. hi your tutorials is very good,
    please keep it up, so new person like me on magento can learn from industries leaders.
    if you already doing that, please share a link , so i can find.
    now days i need to learn how to make a extensions in magento,
    your any help is appreciate

  18. I have write the same code and using the magento 1.9, its showing 404 error, please provide your suggetsion regarding this.

    Thanks

    1. try this..
      1) go to Admin->System->Cache Management
      2) select all the Cache Type
      3) Keep action as Refresh and hit submit.
      Now try loading your module.
      If any queries..let me know 🙂

    2. remove the cache . var – folder.inside remove all file .then check…

  19. I am using Magento 1.7.0.0. And whenever i m trying to upload my custom extension, it is giving me :-

    CONNECT ERROR: Package file is invalid
    Invalid package name, allowed: [a-zA-Z0-9_-] chars
    Invalid version, should be like: x.x.x
    Invalid stability
    Invalid date, should be YYYY-DD-MM
    Invalid channel URL
    Empty authors section
    Empty package contents section

    *PLEASE guide me how to get rid of this. I ll b highly obeliged. :'( 🙁
    Thanks..

  20. Thx a lot, i need simply to add this remark:
    clear the content of folder /var/cache after each modification to see result.

  21. Hello. This tutorial taught me a lot. I’m wondering if there’s a continuation to this about other parts of the extension specially the Blocks and also about layouting.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.