Writing a Custom Module
Creating a custom module is quite easy but it does involve several steps. This post describes the steps that need to be followed to create a new module.
STEP ONE: (Creating the Directory Structure)
You will be creating your module in the app/code/local directory. First create a directory and call it ‘MyPackage’. This will be your new package where you can create modules. (Similar to Mage Package). Next create a directory and name it ‘MyModule’. This is your Module directory which will contain the required files like models, blocks, controller,etc..
STEP TWO: (Registering the Module)
You need to inform Magento that you have a custom module.. This is done by creating an XML file in /app/etc/modules, MyPackage_All.xml. Notice the _All in the xml file name. you can declare all of my modules here.
<?xml version=”1.0″?>
<config>
<modules>
<MyPackage_MyModule>
<active>true</active>
<codePool>local</codePool>
</MyPackage_MyModule>
</modules>
</config>
This code is used to inform Magento that there is an active module (you can turn it off from here by setting ‘active’ to false). Also that it is located in the ‘local’ code pool.
After performing this step you can check the result by going to System->Configuration->Advanced. You will see a list of available modules and in that list MyPackage_MyModule will be present.

STEP THREE: (Creating a Block Class)
Next we need to create the module. Go to the app/code/local/MyPackage/MyModule Directory and create a directory called ‘Block’. Create a php file called View.php and copy the following code:
<?php
class MyPackage_MyModule_Block_View extends Mage_Core_Block_Template {
public function myFunction(){
return “Hello World!“;
}
}
?>
We have created a simple block which contains a function that returns the string ‘Hello World’.
STEP FOUR: (Enter Config info for module)
Now we need to make a config.xml file in app/code/local/MyPackage/MyModule/etc directory. This is done to enter configuration information for the module. Copy the following contents:
<?xml version=”1.0″ ?>
<config>
<modules>
<MyPackage_MyModule>
<version>0.1.0</version>
</MyPackage_MyModule>
</modules>
<global>
<blocks>
<mymodule>
<class>MyPackage_MyModule_Block</class>
</mymodule>
</blocks>
</global>
</config>
This informs Magento of the module version. Version matters when you set up your module to be update-able. (A newer version will inform Magento to run the update files if you have them).
It also informs Magento that the module contains block files which are found in MyPackage/MyModule/Block/ directory. The class name should be “MyPackage_MyModule_Block”.
STEP FIVE: (Creating a Template file)
Now we need to create a template file which uses the View Block Class to display the message “Hello World”. Create a template (phtml) called view.phtml in the following directory: app\design\frontend\default\default\template\mymodule\view.phtml.
<div><span><strong>This is the output of the MyModule module:</strong></span><br /> <span style=”color:#FF9933;”>
<?php
echo $this->myFunction();
?>
</span>
</div>
This just outputs some HTML and also runs the myFunction() function from our block (View.php).
STEP SIX: (Using the Module)
The module is now ready for use. You can view the output by two ways.
One way is to add the following code in a cms page:
{{block type=”mypackage_mymodule/view” template=”mymodule/view.phtml” }}
Another way is to enter it as a layout update:
<reference name=”right”>
<block type=”mymodule/view” name=”mymodule” template=”mymodule/view.phtml”/>
</reference>
RESULT:
This will be the final output of your module:






