Magento - Create a Magento module (Part I)

Modules are the basic building block of Magento a site. Each Module is a collection of code that performs related and integrated tasks. For instance there is a module dedicated to calculating taxes. Another module handles everything related to the Wishlist features of Magento. All modules follow the same structure. This page describes how to build a very simple Magento module that displays some text using a custom Magento module. The result will look like this:



Step I - Set up a directory structures for your module.

Note that this code is tested on Magento community 1.7 and 1.8. I cannot provide any assurances that it will work on older versions.

You will need a directory to hold your modules. This directory goes in the app/code/local directory. This directory is recommended for your custom development projects. More on Magento directories here

Below I created a module called Msc. I will put all my modules in this directory. Sometimes this directory is called a namespace. You should give this namespace directory a fairly unique name such as your domain name, or your company name. Under this directory you will put your modules. Below I created a module named Module1. Under each module, you should have a set of directories with these names:

  • Block
  • controllers
  • etc
  • Helper
  • Model
  • sql

directory structure for a custom Magento module called Model1

Step II - set up your module's xml file.

Next we will want to create an XML file that provides Magento with basic information about your module. This name is made up of the namespace name plus the module name with an underscore between the two. Watch the syntax and case, because Magento can be very picky about this.

New configuration file called Msc_Module1.xml

contents of file Msc_Module1.xml -

<?xml version="1.0"?>
<!-- Filename:  \app\etc\modules\Msc_Module1.xml -->
<config>
     <modules>
        <Msc_Module1>
            <active>true</active>
            <codePool>local</codePool>
        </Msc_Module1>
     </modules>
</config>

Step III  create a config and controller for our module

Every module requires a config.xml file. This is placed in the modules etc directory.
In this case the file should be placed in directory \app\code\local\Msc\Module1\etc

<?xml version="1.0"?>
<config>
    <frontend>
        <routers>
            <!-- the <module1> tagname appears to be arbitrary, but by convention is should match the frontName tag below-->
            <module1>
                <use>standard</use>
                <args>
                    <module>Msc_Module1</module>
                    <frontName>module1</frontName>
                </args>
            </module1>
        </routers>
    </frontend>
</config>

Finally we need to set up a controller. Magento uses methods loadLayout() and renderLayout() to load and display the framework of the Magento site – that is the basic head, body columns and footer of the site.

<?php
 /*
 filename: \app\code\local\Msc\Module1\controllers\IndexController.php

 To call this module, try this syntax:
 MAGENTOSHOP/frontName/actionControllerName/actionMethodName/
 http://mySite.com/module1 or http://mySite.com/index.php/module1   
 */

class Msc_Module1_IndexController extends Mage_Core_Controller_Front_Action{

    public function indexAction(){
          echo ' My Index Action';
          
		  // load the layout/*.xml files 
		  $this->loadLayout();          
		  
		  // create basic layout
		  $this->renderLayout();
    }    
}
 

To call this module, try this syntax:
MAGENTOSHOP/frontName/actionControllerName/actionMethodName/

http://mySite.com/module1  or 
http://mySite.com/index.php/module1

Don't forget to turn off or clear Magento Cache before viewing output.

The results may not win any awards, but should look like this if everything is set up properly.

Here is a zipped file of the whole module.

Here is another module that gets a parameter from url and stores in cookie