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
<br />
<?xml version="1.0"?><br />
<config><br />
<modules><br />
<M4U_HelloWorld><br />
<active>true</active><br />
<codePool>local</codePool><br />
</M4U_HelloWorld><br />
</modules><br />
</config><br />
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.
19 responses so far ↓
mage_ezf // June 18, 2009 at 1:05 pm |
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.
Moinul Islam // June 19, 2009 at 12:30 pm |
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.
sagi // June 20, 2009 at 3:00 am |
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.
Moinul Islam // June 20, 2009 at 9:58 am |
OK, how can i help you?
Sagi // June 21, 2009 at 6:58 am |
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.
Moinul Islam // June 22, 2009 at 7:34 pm |
Here i have been uploaded new module source code in left panel “box” section. Please download.
Accilies // June 30, 2009 at 2:10 pm |
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/
Moinul Islam // June 30, 2009 at 5:11 pm |
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.
Accilies // July 1, 2009 at 4:27 am
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?
Accilies // July 1, 2009 at 6:34 am
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
Legend // July 1, 2009 at 3:48 pm |
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.
Legend // July 1, 2009 at 4:13 pm |
i downloaded the zip file and uploaded it into my folder . But still am getting the 404 error..
Moinul Islam // July 1, 2009 at 7:36 pm |
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
Jameel Khan // September 8, 2009 at 1:06 am |
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
Ankit // October 12, 2009 at 6:21 pm |
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?
Ankit // October 12, 2009 at 6:23 pm |
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.
Moinul Islam // October 12, 2009 at 11:45 pm |
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.
supunmali // October 20, 2009 at 12:59 pm |
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
Bogomips // October 23, 2009 at 2:49 pm |
I did use your exact guidelines. But I get the 404 Not found error.
version could be a problem?
thanks