On this example we make the following Magento page generated by a custom module with template and layout file.
Step 1 – Set up a directory structures for your module.
See Tutorial I for more details about this step. This module will be stored in directory:
\app\code\local\Msc\Module4\
Step 2 set up your module’s xml file.
file: \app\etc\modules\Msc_Module4.xml
<?xml version="1.0"?>
<!-- Filename: \app\etc\modules\Msc_Module4.xml -->
<config>
<modules>
<Msc_Module4>
<active>true</active>
<codePool>local</codePool>
</Msc_Module4>
</modules>
</config>
Step 3 – create config file for our module
Every module requires a config.xml file. This is placed in the module’s etc directory.
\app\code\local\Msc\Module4\etc\config.xml
<?xml version="1.0"?>
<config>
<frontend>
<routers>
<router4frontend>
<use>standard</use>
<args>
<module>Msc_Module4</module>
<frontName>module4</frontName>
</args>
</router4frontend>
</routers>
<layout>
<updates>
<module4>
<file>module4.xml</file>
</module4>
</updates>
</layout>
</frontend>
<global>
<blocks>
<module4>
<class>Msc_Module4_Block</class>
</module4>
</blocks>
</global>
</config>
Step 4 – create a block for your module
in file:- \app\code\local\Msc\Module4\Block\myblock4.php
<?php
class Msc_Module4_Block_Myblock4 extends Mage_Core_Block_Template
{
public function method4block()
{
return 'output from method-4 block!!' ;
}
}
Step 5 – create a controller file for your module
in file - \app\code\local\Msc\Module4\controllers\IndexController.php
<?php
// to test this module:
// http://myurl.com/module4/
// http://myurl.com/module4/index/
// http://myurl.com/module4/index/mymethod
class Msc_Module4_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function mymethodAction()
{
echo 'perform method mymethod';
}
}
Step 6 – create a template for your module
If you have a custom theme, then you can store this file in your custom theme:
\app\design\frontend\default\YOURTHEME\template\module4\template4.phtml
If you are lost and have no idea what I am talking about, use this directory/file
\app\design\frontend\base\default\template\module4\template4.phtml
then read up on how to create a custom theme.
<?php // this has to match with method name in /Block/myblock4.php echo $this->method4block();
Step 7 – Create a layout file in XML
put this file in in \app\design\frontend\default\YOURTHEME\layout\module4.xml or
\app\design\frontend\base\default\layout\module4.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="content">
</reference>
</default>
<router4frontend_index_index>
<reference name="content">
<block type="module4/myblock4" name="anyname_unique_name_goes_here_2468" template="module4/template4.phtml" />
</reference>
</router4frontend_index_index>
</layout>