Magento : Create Custom Magento Module – Tutorial

0
24
views
Advertisement

The first thing in creating magento module is to decide the codepool. The magento app/code directory has the three Magento code pools (core, community, local).

app/code/

community/

core/

local/ 

The second thing in creating a custom module is to determine the package name(namespace) and module name. We can place different modules in namespace. The benefit of creating namespace is that we can have two modules with same name but placed in different namespaces. Core magento modules are placed in Mage namespace.

1. Place your custom module in :

app/code/local/PackageName

Package name should be unique.

Package Name : Myfirst

Module Name : Module


2. Create  a directory structure for this module:

app/code/local/Myfirst/Module/Block
app/code/local/Myfirst/Module/controllers
app/code/local/Myfirst/Module/etc
app/code/local/Myfirst/Module/Helper
app/code/local/Myfirst/Module/Model
app/code/local/Myfirst/Module/sql

 
3. Create xml file to activate the module (app/etc/modules/Myfirst_ Module.xml):

<config>
  <modules>
    <Myfirst_Module>
      <active>true</active>
      <codePool>local</codePool>
    </Myfirst_Module>
  </modules>
</config>


Either we can create app/etc/modules/Myfirst_ All.xml . _All.xml file can be used for all the modules inside Myfirst namespace.

4. Create a configuration file for the module (app/code/local/Myfirst/Module/etc/config.xml):
 

<config>
  <modules>
    <Myfirst_Module>
      <version>0.1.0</version>
    </Myfirst_Module>
  </modules>
</config>

a. Set up module’s router information in config.xml.

<config>
 
  <!– … –>
 
  <frontend>
    <routers>
      <module>
        <use>standard</use>
        <args>
          <module>Myfirst_Module</module>
          <frontName>module</frontName>
        </args>
      </module>
    </routers>
  </frontend>
 
  <!– … –>
 
</config>

 
Create controller file app/code/local/Myfirst/Module/controllers/IndexController.php

 
<?php

class Myfirst_Module_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction(){
  
        $this->loadLayout();
       
        $this->renderLayout();
  
    }
  
    public function viewAction(){
  
        $this->loadLayout();
       
        $this->renderLayout();
  
    }
}

 
For the index action url will be like http://website.com/module/index/index

And for view action url will be http://website.com/module/index/view

b. Set up block information in config.xml

<config>
  <frontend></frontend>
 
  <!– … –>
 
  <global>
    <blocks>
      <module> <class>Myfirst_Module_Block</class> </module>
    </blocks>
  </global>
 
  <!– … –>
 
</config>

 
Create a Block class app/code/local/Myfirst/Module/Block/brand.php
 

<?php

class Myfirst_Module_Block_Brand extends Mage_Core_Block_Template
{

// All necessary methods

}
 
Not like other MVCs, magento views do not have only template files. But, Magento view has block (.php) files and template (.phtml) files. Each block has one template file. And views don’t access the data object from controller files. Rather, the views can directly call models for information it needs to display. In template $this refers to the template’s block object. Template file calls functions in block file and block file can call function in model file. Every module use different layout file and which layout file will be used by module in defined in module’s config.xml

c.  Set up Layout XML files in config.xml

<config>
  <frontend>
    <routers>
      <module>
        <use>standard</use>
        <args>
          <module>Myfirst_Module</module>
          <frontName>module</frontName>
        </args>
      </module>
    </routers>
    <!– … –>
    <layout>
      <updates>
        <module>
          <file>module.xml</file>
        </module>
      </updates>
    </layout>
    <!– … –>
  </frontend>
</config>


Define page layout in app/design/frontend/Yourtheme/default/layout/module.xml

<layout version=”0.1.0″>
  <default></default>
  <module_index_index>
    <reference>
      <block template=”module/brand.phtml” />
    </reference>
  </module_index_index>
</layout>

Create template file in app/design/frontend/ Yourtheme/default/template/module/brand.phtml

Ok, we are done !!! Please leave your comments here. 🙂

I hope this helps someone 🙂

Thanx,
Mayank Zalavadia  🙂