How to build a hello world module for Magento

Create your first “Hello World” magento module with the following steps.

  1. Create a folder structure for your module:
    • app/code/local/{ModuleNamespace},
    • app/code/local/{ModuleNamespace}/{ModuleName}
    • app/code/local/{ModuleNamespace}/{ModuleName}/etc
    • app/code/local/{ModuleNamespace}/{ModuleName}/controllers
  2. Configure your module: create a config.xml file in app/code/local/{ModuleNamespace}/{ModuleName}/etc/ directory
  3. Create a controller file IndexController.php in your controllers directory
  4. Activate your module: create a {ModuleNamespace}_{ModuleName}.xml file in app/etc/modules/
  5. Clear the Magento cache
  6. Visit http://domain.com/yourUrl

Right now, we will explore these steps in details:

1) First, create a module’s folder structure:

  • app/code/local/Wdevelop,
  • app/code/local/Wdevelop/HelloWorld
  • app/code/local/Wdevelop/HelloWorld/etc
  • app/code/local/Wdevelop/HelloWorld/controllers

2) Next, we’ll create the configuration file of the module: config.xml inside the folder app/code/local/Wdevelop/HelloWorld/etc

<?xml version="1.0"?>
<config>
    <modules>
        <Wdevelop_HelloWorld> <!-- {ModuleNamespace}_{ModuleName} -->
            <version>0.1.0</version><!-- Module version -->
        </Wdevelop_HelloWorld>
    </modules>
    <frontend>
        <routers>
            <helloworld> <!-- {modulename} -->
                <use>standard</use> <!-- [standard|admin], we use standard because it's the frontend -->
                <args>
                    <module>Wdevelop_HelloWorld</module> <!-- {ModuleNamespace}_{ModuleName} -->
                    <frontName>helloworldurl</frontName>  <!-- This is the URL of the module. i.e magento.local/helloworldurl will be the url of your module. -->
                </args>
            </helloworld>
        </routers>
    </frontend>
</config>

3) Then, we will create the Controller file. So the method indexAction() will be called for following urls:

  • yoursite.com/helloworldurl
  • yoursite.com/helloworldurl/index
  • yoursite.com/helloworldurl/index/index
<?php
/*
class {ModuleNamespace}_{ModuleName}_{Controllername}Controller
 extends Mage_Core_Controller_Front_Action
*/
class Wdevelop_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "Hello World";
    }
}

4) Last file we will create is Wdevelop_HelloWorld.xml in the location app/etc/modules/.  We do it to inform Magento that our module exists

<?xml version="1.0"?>
<config>
    <modules>
        <Wdevelop_HelloWorld>   <!-- Company_ModuleName -->
            <active>true</active>  <!-- Whether module is active -->
            <codePool>local</codePool> <!-- code pool location: app/code/local -->
        </Wdevelop_HelloWorld>
    </modules>
</config>

5) Clear the Magento cache

Admin Panel > System > Cache Management > Flush Magento Cache

6) Ensure the HelloWorld page shows up

There is the the video tutorial:

One thought on “How to build a hello world module for Magento

  1. Pingback: Как создать "Hello world" модуль для Magento - Технарь

Leave a Reply

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