In this tutorial for beginner, we will learn how to create basic Helloworld Magento module from scratch. There are 2 steps to be done:
– Declare module
– Create folders of module
Now, let’s start to build your very first Magento extension!
1. Declare module in Magento
First, we need to create config.xml file in app/etc/modules/Magentoexplorer_Helloworld.xml, in this tutorial, my package is Magentoexplorer and module’s name is Helloworld (I will explain more later)
Now, put the following code in config.xml:
1 2 3 4 5 6 7 8 9 | <?xml version="1.0"?> <config> <modules> <Magentoexplorer_Helloworld> <active>true</active> <codePool>local</codePool> </Magentoexplorer_Helloworld> </modules> </config> |
To check if the module is properly displayed in Magento backend, go to System > configurations > Advanced > Advanced > Disable Modules Output
2. Create folder structures of Module
In order to create a Magento module, you should create the following folder structure
Please note that capitalization of directories and files are very important. You should name directories and files exactly as above.Otherwise, the module will not work properly and show the text “Hello”
After finishing directory structure, we will create the following files and put inside those directories.
First, create config.xml in app/code/local/Magentoexplorer/Helloworld/etc/config.xml, the file contains the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0"?> <config> <modules> <Magentoexplorer_helloworld> <version>0.1.0</version> </Magentoexplorer_helloworld> </modules> <frontend> <routers> <helloworld> <use>standard</use> <args> <module>Magentoexplorer_Helloworld</module> <frontName>helloworld</frontName> </args> </helloworld> </routers> </frontend> </config> |
The code above will notify Magento system about the code that will run, frontend node declare router in front-end.
Now, enter this link to your browser localhost/magento/helloworld (magento as root directory) and see the result.
Error? It’s alright, it’s due to the router we declared above could not find controller. What we need to do now is create controller for our module
Create indexController.php in app/code/local/Magentoexplorer/Helloworld/controllers/IndexController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php class Magentoexplorer_Helloworld_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { echo 'index function'; } public function addAction() { echo 'add function'; } public function editAction() { echo 'edit function'; } public function deleteAction() { echo 'delete function'; } } |
We created a class that is extended from Mage_Core_Controller_Front_Action , class Magentoexplorer_Helloworld_IndexController  is named according to file path (this is Magento’s rule).
3. Run your module for the first time
Now, try to re-enter the link localhost/magento/helloworld in your browser and check the result!
Yay, you will see: Hello World,Â
Congratulation, you’ve just created your first Magento module Helloworld.
If you want to enter other actions like: add, edit, delete, you just need to add index/action_name (zend framework rule:”name_function+Action”), for example: localhost/magento/helloworld/index/add
So, what are block, helper, model folders? Well, we will explain more in the next part, you will also learn how to create layout for your extension.
If you have any troubles, download the module I created here: Helloworld_magento_module by Magentoexplorer.com
We hope our tips will be useful for beginning working with Magento. Please do not hesitate to ask us any additional questions or add your feedback.
7 Comments
Thank you for this sweet tutorial, I have successfully made my very first Magento module from scratch. Please keep posting tutorial about Magento module.
John
Hello,
for some reason I’m getting “index function” on a white screen and that’s it. I have double checked the code – everything is like in your tutorial.
Best regards,
Vlad.
I had write code in magento1.9.0.1 but still it is not working and provide us 404 errors
Hi, It was awesome tutorial, But i got the message displayed in Blank Page, currently am using magento 1.9 version. I can understand that procedure for 1.9 is slight different than your tutorial. Can you expand this concept to further like how to display as in screen shot in Magento 1.9 version.
Thanks in advance 🙂
Hello, can you turn on debug and send me the error message?
The above tutorial will only result in the “Index Action” text displayed on a white background since no layouts are defined!
I had write code in Magento ver. 1.9.2.3 but still it is not working and provide us 404 errors