Create first Magento 1.x module

This article will help you to build your first full functional Magento module.

Define the module

First step is to define the module and the location of it in the code pool folder /app/etc/modules/<Namespace>_<Module>.xml

<?xml version="1.0"?>

<config>
    <modules>

         <Bb_Core>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
            </depends>
        </Bb_Core>


    </modules>
</config>

 

Create Controller

/app/code/local/<Namespace>/<Module>/controllers/IndexController.php
Create the controller in the module folder under the controllers directory.

<?php
class <Namespace>_<Module>_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
            $this->loadLayout();
            $this->renderLayout();
    }
}

Controllers (the classes under under the controllers folder ) are not auto loaded in Magento, so you have to include the extended class manually.

Create Configuration XML

/app/code/local/<Namespace>/<Module>/etc/config.xml

You have to define this file to configure and define all resources needed in the module.

You can define into the configuration XML file the routes, blocks, models, translations and others as shown in the following example:

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <version>0.1.0</version>
        </[Namespace]_[Module]>
    </modules>
    <frontend>
        <routers>
            <[module]>
                <use>standard</use>
                <args>
                    <module>[Namespace]_[Module]</module>
                    <frontName>[module]</frontName>
                </args>
            </[module]>
        </routers>
        <layout>
            <updates>
                <[module]>
                    <file>[module].xml</file>
                </[module]>
            </updates>
        </layout>
    </frontend>   
    <global>
        <models>
            <[module]>
                <class>[Namespace]_[Module]_Model</class>
                <resourceModel>[module]_mysql4</resourceModel>
            </[module]>
            <[module]_mysql4>
                <class>[Namespace]_[Module]_Model_Mysql4</class>
                <entities>
                    <[module]>
                        <table>[module]</table>
                    </[module]>
                </entities>
            </[module]_mysql4>
        </models>
        <resources>
            <[module]_setup>
                <setup>
                    <module>[Namespace]_[Module]</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </[module]_setup>
            <[module]_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </[module]_write>
            <[module]_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </[module]_read>
        </resources>
        <blocks>
            <[module]>
                <class>[Namespace]_[Module]_Block</class>
            </[module]>
        </blocks>
        <helpers>
            <[module]>
                <class>[Namespace]_[Module]_Helper</class>
            </[module]>
        </helpers>
    </global>
</config>