Ads

Tuesday, June 22, 2010

Create custom module in magento

1) Create directory app\code\local\mymodulepack. Here ‘mymodulepack’ will be the new directory folder where you can create modules.
2) Create module named ‘MyModule’ in above defined directory path. This will be your module directory which will contain required files like models, blocks, controllers etc.
3) Now add your new module xml file in app/etc/modules/ mymodulepack _All.xml. Consider that _All.xml will be used to add all new module definitions to the same file instead of cluttering the etc/modules folder with many files.
4) Following defined 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.
<?xml version="1.0"?>
<config>
<modules>
<mymodulepack_MyModule>
<active>true</active>
<codePool>local</codePool>
</mymodulepack_MyModule>
</modules>
</config>
5) Now you can enable or disable this module at Admin -> System -> Advanced. Here you can see your new module ‘Mymodulepack_MyModule’ listed there with status enable.
Note : Please disable the cache from admin section to view the module.
6) And now create the block php class file at app/code/local/mymodulepack/MyModule/Block/Myview.php which contains a function that returns the string ‘Hello World’.
<?php
class mymodulepack_MyModule_Block_Myview extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$this->setTemplate('mymodule/myview.phtml');
}
protected function _prepareLayout()
{
parent::_prepareLayout();
}
}
?>
7) Now make a config.xml file in app\code\local\mymodulepack\MyModule\etc directory. This is done to enter configuration information for the module.
<?xml version="1.0" encoding="iso-8859-1"?>
<config>
<modules>
<mymodulepack_MyModule>
<version>0.1.0</version>
</mymodulepack_MyModule>
</modules>
<blocks>
<MyModule>
<rewrite>
<MyModule>mymodulepack_MyModule_Block_Myview</MyModule>
</rewrite>
</MyModule>
</blocks>
<frontend>
<routers>
<MyModule>
<use>standard</use>
<args>
<module>mymodulepack_MyModule</module>
<frontName>MyModule</frontName>
</args>
</MyModule>
</routers>
<layout>
<updates>
<MyModule>
<file>MyModule.xml</file>
</MyModule>
</updates>
</layout>
</frontend>
<global>
<helpers>
<extras>
<class>mymodulepack_MyModule_Helper_Data</class>
</extras>
</helpers>
</global>
</config>
8) Create the php class file at app/code/local/mymodulepack/MyModule/ Helper / Data.php
<?PHP
class mymodulepack_MyModule_Helper_Data extends Mage_Core_Helper_Abstract {
}
?>
9) 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 myview.phtml in the following directory:
app/design/frontend/default/your theme/template/mymodule/myview.phtml.
<div>
<span style=" color:#0000FF; font:Arial, Helvetica, sans-serif;">
<bold><?php
echo "Hello World";
?></bold>
</span>
</div>
10)The module is now ready for use.
Where do I put this block? Should I create new mymodule.xml in theme/layout?
You can view the output by two ways.
1) {{block type=”mymodulepack_mymodule/view” template=”mymodule/myview.phtml” }}
call this block in page.xml file
2) Create xml file in app/design/frontend/default/your theme/layout/MyModule.xml
<?xml version="1.0"?>
<layout>
<default>
<reference name="content">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
</reference>
<reference name="content">
<block type="core/template" name="mymodule" template="mymodule/myview.phtml" />
</reference>
</default>
</layout>
Note : Set the position of your module as per your requirement,
1. <reference name="right">
2. <reference name="left">
3. <reference name="content">

No comments:

Post a Comment