Magento module

Modules are the core functioning unit in a Magento system. Here I am giving an overview of how to write a “Hello World” module in magento.

Simple basics before start you cooking :-)

At the time of instantiating, magento system reads all the Xmls inside the folder /app/etc/modules/ to get the list of active modules and their corresponding code repository . So very first step of creating a magento module is to declare the module and its core repository(the repository of the module, in magento it’s calles codepool ).

If you open /app/code/ you will see 3 coodpool :- core, community and local.

app/
|–code/
| |—community/
| |—core/
| |—local/
|

The “core” cood pool is dedicated for magento’s all system modules. The “community” codepool is for all modules developed by magento’s partners and local is the codepool dedicated for you as a developer to write your own customized code.

In Magentos’ all modules are organized under a package. This package is nothing but a simple folder under codepool containing different modules. For example all core modules of magento system is kept unde package “Mage” (open the folder /app/code/code/ , you see the folder “Mage” under which all system module resides).

Before start cooking lets fixup the name of our module is Mymodule and our package name is Mypackage .

1.Declare your shell module and it’s code pool

Create a xml file /app/etc/modules/Mymodule.xml (You can use any name, even you can use a single file to declare number of modules).

   <?xml version="1.0"?>
   <config>
      <modules>
         <Mypackage_Mymodule>
            <active>true</active>
            <codePool>local</codePool>
         </Mypackage_Mymodule>
    </modules>
  </config>

2.Create the basic structure Under /app/code/core/local/ :-

Mypackage/
|–Mymodule/
| |–Block/
| |–controllers/
| |–etc/
| |–Helper/
| |–sql/
|

3.Write the front controller /controllers/IndexController.php

<?php

   class Mypackage_Mymodule_IndexController extends Mage_Core_Controller_Front_Action
   {
      public function indexAction()
      {

         //Do nothing fo the time being

         $this->loadLayout();
         $this->_initLayoutMessages('customer/session');
         $this->renderLayout();
      }
   }

4.Create design layout for our controller action
/app/design/frontend/default/default/layout/mymodule.xml

   <?xml version="1.0"?>
   <layout version="0.1.0">
      <default>
      </default>
      <mymodule_index_index>

         <remove name="right"/>
         <remove name="left"/>

         <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
         </reference>

         <reference name="content">
            <block type="mymodule/helloworld" name="helloworld" template="mymodule/helloworld.phtml"/>
         </reference>
      </mymodule_index_index>
   </layout>

4.Create associated block
/Block/Helloworld.php

<?php
   class Mypackage_Mymodule_Block_Helloworld extends Mage_Core_Block_Template
   {
      public function getHelloWorldMsg()
      {
         return "Hello world";
      }
   }

5.Create Helper for this module
/Helper/Data.php


<?php

   class Mypackage_Mymodule_Helper_Data extends Mage_Core_Helper_Abstract
   {

   }

6.Now organize every thing write the settings for this module in
/etc/config.xml

We declare the following things in out config.xml

i) Version number of the module. Magento keep track to version number in “core_resource” table which is used to run sql_setup[Details discussed in another post] .

ii) Router information for this module. It contains frontanme and access privilege (front end controller or admin ).

iii) Which layout xml to use for its design.

iv) Global path for module , blocks and helper .

v) Default connection to use for db read & write.


<?xml version="1.0"?>
   <config>
      <!-- i.Version Number -->
      <modules>
         <Mypackage_Mymodule>
            <version>0.1.0</version>
         </Mypackage_Mymodule>
      </modules>
      <!-- ii. Router information -->
      <frontend>
         <routers>
            <mymodule>
                 <!-- Means front end controller -->
                  <use>standard</use>
                  <!-- Fron name "Myfrontname" declared for "Mypackage_Mymodule" -->
                  <args>
                     <module>Mypackage_Mymodule</module>
                     <frontName>myfrontname</frontName>
                  </args>
            </mymodule>
         </routers>
         <!-- iii. Desing layout file -->
         <layout>
            <updates>
               <mymodule>
                  <file>mymodule.xml</file>
               </mymodule>
            </updates>
         </layout>
      </frontend>

      <global>
         <!-- iv. Global path for model, blocks, helper -->
         <!-- models reside under path "Mypackage/Mymodule/model/" -->
         <models>
            <mymodule>
               <class>Mypackage_Mymodule_Model</class>
            </mymodule>
         </models>
         <!-- blocks reside under path "Mypackage/Mymodule/Blocks/" -->
         <blocks>
            <mymodule>
               <class>Mypackage_Mymodule_Block</class>
            </mymodule>
         </blocks>
         <!-- Helper reside under path "Mypackage/Mymodule/Helper/" -->
         <helpers>
            <mymodule>
               <class>Mypackage_Mymodule_Helper</class>
            </mymodule>
         </helpers>
         <!-- v. Declaring db connections -->
         <!-- Also sql setup handler declared in this portion -->
         <resources>
            <mymodule_write>
               <connection>
                  <use>core_write</use>
               </connection>
            </mymodule_write>

            <mymodule_read>
               <connection>
                  <use>core_read</use>
               </connection>
            </mymodule_read>
         </resources>

      </global>
   </config>

That’s it. Go to http://yourdomain.come/magentopath/myfrontname/ and see your first magento modules output :-)

About these ads