Create new module “HelloWorld” – in Magento

Do you want to create a new page in Magento ? or Do you want to create a new module in Magento ? If yes, Then ok, just spend 10 minutes and follow below steps.

Objectives: I will create a new page in Magento whose output will be “Hello World ! I am a Magento Guy..”.

Target: Create a new module called “HelloWorld”

Step 1: Module Declaration

Create app/etc/modules/M4U_HelloWorld.xml and write below code

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

Step 2: Module Configuration

a. Create a controller class app/code/local/M4U/HelloWorld/controllers/IndexController.php

class M4U_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
     $this->loadLayout(array('default'));
     $this->renderLayout();
    }
}

b. Create a Block class app/code/local/M4U/HelloWorld/Block/HelloWorld.php

class M4U_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template
{
  // necessary methods
}

c. create configuration xml in app/code/local/M4U/HelloWorld/etc/config.xml

<?xml version="1.0"?>
<config>
	<global>
		<modules>
                <m4u_helloworld>
                        <version>0.1.0</version>
                </m4u_helloworld>
        </modules>
	<blocks>
            <helloworld>
                <rewrite>
		 <helloworld>M4U_HelloWorld_Block_HelloWorld</helloworld>
		</rewrite>
            </helloworld>
	 </blocks>

        </global>
	   <frontend>
                <routers>
                        <helloworld>
                                <use>standard</use>
                                <args>
                                      <module>M4U_HelloWorld</module>
                                      <frontName>helloworld</frontName>
                                </args>
                        </helloworld>
                </routers>
		<layout>
			<updates>
				<helloworld>
		   		      <file>helloworld.xml</file>
				</helloworld>
			</updates>
       		</layout>
        </frontend>
</config>

Define Frontend Template :

1. Define page layout in app/design/frontend/M4U/default/layout/helloworld.xml

N.B: Use default instead of M4U as template location if you use default design packages. Means create file in app/design/frontend/default/default/layout/helloworld.xml

<?xml version="1.0"?>

	<layout version="0.1.0">

		<helloworld_index_index>
			<reference name="root">
				<action method="setTemplate"><template>page/1column.phtml</template></action>
       	 	</reference>
			<reference name="content">
            	<block type="helloworld/helloworld" name="hello" template="helloworld/helloworld.phtml"/>
        	</reference>
		</helloworld_index_index>

	</layout>

2. Create template file app/design/frontend/M4U/default/template/helloworld/helloworld.phtml and write down

N.B: Use default instead of M4U as template location if you use default design packages. Means create file in app/design/frontend/default/default/template/helloworld/helloworld.phtml

Hello World ! I am a Magento Guy..

Hey, new module is ready to run and hit browser with url http://127.0.0.1/projectname/index.php/helloworld/

and see result.

That’s it……..

Is it easy or not ? what you think ?

Coder, Please at first try yourself.

Ohh.., Still are you facing problem ? Lets download full module source code from left panel “BOX” section or let me know.

Note: I have upgraded HelloWorld module source code of BOX widget. So it will resolve the problem which was faced by some guys.

48 Responses to Create new module “HelloWorld” – in Magento

  1. Hi,
    Its not working with me. I have configure all the things as mention. But it doesn’t show “Hello World ! I am a Magento Guy..”.
    It seems IndexController does work properly it calls default layout of megento. Url hellowrold working fine but output is as per need.

    • Hey, you may placed your design related xml and phtml files in app/design/frontend/M4U/default/ location. But you didnot assign right package name(M4U) in admin. So you should use app/design/frontend/default/default/ as template location. I updated post, please see again. Hope it will work. Otherwise let me know, i can send you code files.

  2. Hi,
    I am having trouble getting this to work on my test store.
    Any chance to get in touch offline and work on it together? I really need to get this running.

  3. I am trying to develop a module – I think I followed all the steps correctly but nothing happens when I hit the URL. It would be great if I can contact you online – I can give you access to it so you can direct me to what is wrong – and off course I will pay you for your time. I could not fund an email to send you information directly.

  4. I tried your example for the frontend only with my namespace but i get 404 error. Folder structure is http://img39.imageshack.us/i/screenshot1azv.png/

    • Your folder structure is ok, but you should assign your new design namespace as design package name in admin under configuration tab. I have tested my source code and works fine. You just try to follow my post, hope you will get result. Otherwise let me know details.

      • Hi Moinul,

        Thanks for the quick response. I happen to find the error…

        When i add the below lines in app/code/local/BW/HelloWorld/etc/config.xml i get the 404 error ELSE, i get a 2 column layout…

        helloworld.xml

        Any idea where is it going wrong?

      • Ok you can delete my previous comments as after following the exact codes, i got the example working… Thank you very much for posting and keep such an informative site online!!

        Regards ,
        Accilies

  5. I like to develop a module.I followed your instructions .But when executing the module in frontend i got a error page in the browser.Could you provide me the right solution.

  6. i downloaded the zip file and uploaded it into my folder . But still am getting the 404 error..

    • Please again see your file structure. I tested source code. I know everything is ok, just try to follow step by step. Hope you will get result. Otherwise explain details

  7. First of all I want to thank the author for his wonderful effort on developing new module in Magento. Guys I have tested this module and It works perfectly. It worked for me in just single attempt. All you need to do is to “Follow the instructions VERY carefully”. Thats exacty what I have done.

    Thanks
    Jameel

  8. Dear i have used the files provided by you, but i am able to access the heeloword module.

    IT is showing Whoops, our bad… error.

    Can you please help me?

  9. also tell me one thing that .phtml file will be placed directly in template folder or we have to create a folder name helloworld and put into that folder.

    • Yes, you have to create new folder name helloworld. I know my code is ok. You follow my instructions and read carefully. Hope you can do it.

  10. I did use your exact guidelines. And tried the source code of yours too (there you have put the .phtml file directly in template folder). Tried in both ways.

    But I get the 404 Not found error.

    I am using foo2 theme. So i did put thoses 2 files in f002 instead of default and tried. but it dont work. Even I did put them in default folder as in yours, but still it dont work, why is it?

    Thanks

  11. I did use your exact guidelines. But I get the 404 Not found error.
    version could be a problem?

    thanks

  12. Pingback: Magento, Magento, Magento « Redbeard Technologies Software and Systems Development Blog

  13. Will try this, thanks for blogging it.

    What might be the cause for some ppl not to be able to get this to work, is all the in Step 1. I’m pretty sure this is not correct. :-)

  14. Hello All, I have updated source code. Hope It will resolve the problems which have been faced by some guys.

    Thanks

  15. This code is working fine without any error.

    Thanks,

  16. I have followed exactly the directions in your post, but I still get 404 error.

    I am using XAMPP on Windows 7 and vhost to modify URL
    from
    http://127.0.0.1/magento/index.php/helloworld
    to
    http://magento.local/index.php/helloworld.

    I am really appreciated if you give me any ideas?

    Thanks,

  17. Sorry, I forgot clear the cache folder. It works perfectly now.
    You can help me by deleting the previous comment.

    Thank you,

  18. Hello all,

    This code is working very fine in fresh magento setup. I have one customized website in magento, In which different package name (Not a default package name). So this module is not working.

    Please give me some solution or advice.

    Thanks in advance,
    Jigs

  19. This module in my system is showing Controller file is loaded but class does not exist

    Can somebody or moil explain what could be the problem .

    But i have do that

    Thanks

  20. Jignesh Can you help , In my default package it is not working , please read the above comment of mine

    Thanks
    Prakash

  21. Anyone can give me his gmail or yahoo id to tell me what could be the problem i would be very thankfull to him

    Thanks
    Prakash

  22. Hi,
    I was followed exactly what you wrote above, but always I was redirect to 404 error page.
    1. Go to System->Cache Management
    2. Select Refresh from the All Cache menu
    3. Click Save Cache settings

    After doing this its stared to work!. You have to add this also in this post for newbies like me.

    Many thank and regards,
    San.

  23. yes, that was the same problem I faced

  24. Prakash,

    I got this error when there were not opening PHP tags () in my IndexController.php and HelloWorld.php files.

  25. Dada, ami Dhrubo. ami khubi bipod-e porci, amar admin-e ekta module add korte hobe, apni to module add kora dekhaichen, kintu oi module ta admin-er menu item hisebe kivabe add korbo dekhan nai,jodi ai bepere help koren tahole vai amar jonne khubi lav hoy,

  26. Thankx to the author….guys its working fyn….initially i got sme problem like whoops ,bad…but then i refreshed cache and everything went alrite……if u r facing the same just refresh ur cache….

  27. Hey man, you have a great blog, I am currently working on the magento project with multiple seller/merchant account facility. I have a very hard time doing all these because I am new to Magento. So I really could use your help. can we work on this together online. it would be a great help for me.

  28. Hey, It works for me, praise the lord. Thanks for all.

    In addition to above steps, I have created page from admin side (CMS->Managepages) and used this code {{block type=”catalog/product_new” template=”helloworld/myview.phtml” }} to view to my custom module in browser.

    I think you are all known about this, since I am new to CMS tools, I didn’t get idea 

  29. Great Tutorial Mate!
    But can you please describe little bit more if we want to add a big module in Administration!
    Actually i am building this getmybrand.com website and need to add new module within its front end and back end!
    Any help would be appreciated!

    Regards,
    Raj

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

  31. Pingback: ::World Web Corner:: Simple for human::

  32. Cassio [Brazil]

    Gratz man! It’s working! Grate job! Very, very thanks!

  33. All work!
    Recache all in adminCP!

  34. Leonardo Cito

    Hello Binod. How are you would how are you getting on with your multi seller /vendor extension in Magento? Please let me know.

  35. Hi , I like your module tutorial but it lacks explanation like in XML files.

  36. you r super.thanks

  37. just follow instruction and download folder.it will work

  38. it is possible to copy the viewAction from CatalogController.php to create new view of view.phtml into newname.phtml i have created copy of viewAction to reveiwAction and also it was run good by using
    http://local/catalog/category/review/id/6
    but i have created new template review.phtml
    then which part we want to change in the catalog.xml file :(

  39. Thank you very much!
    It work well! This tutorial saves me much time ;)

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=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>