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.
48 comments
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.
Thank you for this tutorial. It works for Magento 1.9.1.1 on Mamp.
hi,
is there any way to make url http://shop.local/index.php/inchoo-hellodeveloper/index/sayHello to
http://shop.local/index.php/inchoo-hellodeveloper/sayHello ?
Congratulations on the very interesting tutorial
This is very nice,very clean and easy worked for me
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??
refresh the cache
Thank you, this code works perfect.
Please help, how can i add header and footer code in this which is already fixed?
I am using the same code but my error is 403 i.e Access forbidden!,
so how can i resolve this
it is not working in magento 1.9.2.4
does it works in magento 1.9.2.4
Those who getting 404 error please make sure about the spell of directory name “controllers”. Due to wrong spell you can not do this.
yeah its working on magento 1.9.0.1 thanku for the help….
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.
How to pass variable in this function like
http://shop.local/index.php/inchoo-hellodeveloper/index/sayHello/12
Then how I get the value “12” in function because when I get it like ” public function sayhelloAction($data) ” . Its not working.
I was using 1.9.2 and I struggled for a while too. I had to reinstall, create the files, disable cache and it worked.
Does it work on 1.9.2.3?
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.
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
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.
structure of create module in magento 1.9..? Help me
Whoops, our bad… 404 error
Hi buddy , THIS IS old tutorial , so it will not work in magento 1.9.2.1 as it has new structure.
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
this tutorial is good ….working
good tutorial……..
thanx…………..
Page Not found is coming with every method, tell me the way.
refresh all cache and go on
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
Hi, This tutorial helps me lot. Thank you very very much. Can you Guide to print the same helloworld inside magento layout.
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.
Me too
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 .
I really love your tutorial. After a long struggling i created a module. Keep it up.
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?
you can display on frontend using your url https://inchoo.net/magento/index.php/inchoo-hellodeveloper
is like which you add on frontName in config.xml file
Hi, Let you open config.xml file. You might see inchoo-hellodeveloper. If you wanted to display your module in frontend you would follow this URL: https://inchoo.net/Magento/magento/index.php/inchoo-hellodeveloper <<=this is frontName what you had been specified on to config.xml. I hope you got it.
Regards
Gowthaman
Magento beginner..
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
I have write the same code and using the magento 1.9, its showing 404 error, please provide your suggetsion regarding this.
Thanks
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 🙂
remove the cache . var – folder.inside remove all file .then check…
thanks for the nice article, would love to see more about blocks and front-end stuff for the modules.
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..
Heyy…very nice post….it helped me a lot….Thanks
Thx a lot, i need simply to add this remark:
clear the content of folder /var/cache after each modification to see result.
Please add one more paragraph for Block
hey! thank you so much for sharing this! helped me a lot!
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