Magento Extension Development Part-1

Create Simple “HELLO WORLD” Extension with Magento 1.9

 

In this post we will learn Magento Module configuration in config.xml, Naming classes and files in Magento,Calling methods to create class objects by config and through Mage class. And the basics of creating Magento modules and will create a simple Hello World-like sample. Let’s start…

Module configuration in config.xml

After the module list is loaded, the system will search for file config.xml in the module directory (app/code/[codepool]/[NameSpace]/[ModuleName]/etc/config.xml) to read the configuration of that module. The configurations are placed in tags .

In tags, there are the following tags:

  • modules:

    tags, including configurations for module such as active, depends and version.


<modules>
	<[NameSpace_ModuleName] >
		<active>[true|false] </active>
		<codePool>[core|community|local] </codePool>
		<depends>
			<[NameSpace_ModuleName] />
		</depends>
	<version>(version_number) </version>
	</[NameSpace_ModuleName] >
</modules>
  • global:

    tags, including main configuration for models, resources,blocks, helpers, fieldsets, template, events, eav_attributes, custom_variables. The configurations in this section include basic configuration for module, configuration about folder containing class files for model, block, helper and events that are called (both frontend and admin), configurations for email template and so on.

<global>
	<models>
 		<[ModuleName] >
 			<class>[ClassName_Prefix] </class>
			<resourceModel>[ModuleName] _[resource_model_type] </resourceModel>
 			<[ModuleName] _[resource_model_type] >
 				<! -- definition -->
 			</[ModuleName] _[resource_model_type] >
 			<rewrite><! -- definition --></rewrite>
 		</[ModuleName] >
	 </models>
 	<resources>
 		<[ModuleName] _setup><! -- definition --></[ModuleName] _setup>
 		<[ModuleName] _read><! -- definition --></[ModuleName] _read>
 		<[ ModuleName] _write><! -- definition --></[ModuleName] _write>
 	</resources>
 	<blocks>
	 	<[ModuleName] >
		 	<class>[ClassName_Prefix] </class>
		 </[ModuleName] >
	</blocks>
	<helpers>
 		<[ModuleName] >
 		<class>[ClassName_Prefix] </class>
 		</[ModuleName] >
 	</helpers>
 	<fieldsets>
 		<[page_handle] >
	 		<[ field_name] > ><! -- definition --></[field_name] >
	 	</[page_handle] >
	 </fieldsets>
 	<template>
 		<email>
 			<[email_template_name] module="[ModuleName] " translate="[label][description]">
 				<! -- definition -->
 			<[/email_template_name]>
 		</email>
 	</template>
	<events>
 		<[ event_name] >
	 		<observers><! -- observers --></observers>
	 	</[event_name] >
	 </events>
	 	<eav_attributes><! -- definition --></eav_attributes>
	 <[ModuleName] ><! -- custom config variables --></[ModuleName] >
</global>
  • admin:

    tags including attributes, field sets, routers configuration.
    In this config tag, we usually note about configuration for router of admin.

<admin>
	<attributes>
	 <[ attribute_name] />
	 </attributes>
	 <fieldsets><! -- definition --></fieldsets>
		 <routers>
			 <[ModuleName] >
				 <use>[standard|admin|default] </use>
				 <args>
					 <module>[NameSpace_ModuleName] </module>
					 <frontName>[frontname]</frontName>
				 </args>
			 </[ModuleName] >
		<! -- or --><[ModuleName] >
		 <args>
			 <modules>
				 <[NameSpace_ModuleName]before="[AnotherNameSpace_ModuleName] ">
				 [New_ClassName]
				 </[NameSpace_ModuleName]
			 </modules>
		 </args>
		  </[ModuleName] >
		 </routers>
</admin>

  • adminhtml:

    tags, including configurations of modules in the admin area. The tags are usually layout, translate and events. These configurations only take effect in backend in Magento.

  • <adminhtml>
    	 <events>
    		 <[event_name] >
    		 	<observers><! -- observers --></observers>
    		 </[event_name] >
    	 </events>
    	 <global_search>
    		 <products>
    			 <class>[ModuleName] /search_catalog</class>
    			 <acl>catalog</acl>
    		 </products>
    		 <customers>
    			 <class>adminhtml/search_customer</class>
    			 <acl>customer</acl>
    		 </customers>
    		 <sales>
    			 <class>adminhtml/search_order</class>
    			 <acl>sales</acl>
    		 </sales>
    	 </global_search>
    	 <translate>
    		 <modules>
    			 <[NameSpace_ModuleName] >
    				 <files>
    				 	<default>(name_of_translation_file.csv)</default>
    				 </files>
    		 	</[NameSpace_ModuleName] >
    	 	</modules>
    	 </translate>
    	 <layout>
    		 <updates>
    			 <[ModuleName] >
    			 	<file>[name_of_layout_file.xml] </file>
    			 </[ModuleName] >
    		 </updates>
    	 </layout>
    	 <[ModuleName] ><! -- custom config variables --></[ModuleName] >
    </adminhtml>
    
    • frontend:

    • tags containing active configuration of module on frontend. It includes secure_url, events, routers, translate and layout tags. These configurations only take effect on frontend area.

    <frontend>
    	 <secure_url>
    	 	<[page_handle] >/relative/url</page_handle>
    	 </secure_url>
    	 <events>
    		 <[event_name] >
    		 	<observers><! -- observers --></observers>
    		 </[event_name] >
    	 </events>
    	 <routers>
    		  <[ModuleName] >
    			 <use>[standard|admin|default] </use>
    				 <args>
    					 <module>[NameSpace_ModuleName] </module>
    					 <frontName>[frontname]</frontName>
    				 </args>
    		 </[ModuleName] >
    	 </routers>
    	 <translate>
    		 <modules>
    			 <[NameSpace_ModuleName] >
    				 <files>
    					 <default>
    					 	(name_of_translation_file.csv)
    					 </default>
    				 </files>
    			 </[NameSpace_ModuleName] >
    		 </modules>
    	 </translate>
    	 <layout>
    		 <updates>
    			 <[ModuleName] >
    			 	<file>(name_of_layout_file.xml) </file>
    			 </[ModuleName] >
    	 </updates>
    	 </layout>
    </frontend>
    
    • default:

      tags, containing default configurations of module in the whole stores/websites.

    • stores:

      tags, containing configurations for individual stores. They include <(store_code)> config tag for each store.

  • websites:

    tags, containing configurations for individual websites. They include <(website_code)> config tag for each website.

Naming classes and files in Magento

Controller

File name: [Namecontroller]Controller.php in directories

  • app/code/[Codepool/[Namespace]/[ModuleName]/controllers/
  • app/code/[Codepool]/[Namespace]/[ModuleName]/controllers/Adminhtml/For example:

app/code/local/Masud/Part1/controllers/Part1Controller.php

or

app/code/local/Masud/Bigdeal/controllers/BigdealController.php

or

app/code/local/Masud/Bigdeal/controllers/Adminhtml/BigdealController.php

Class name: [NameSpace]_[Module]_[Namecontroller]Controller and [NameSpace]_[Module]_Adminhtml_ [Namecontroller]Controller. For example:

class Masud_Part1_IndexController extends Mage_Core_Controller_Front_Action{}

or

class Masud_Part1_ Adminhtml_ReportController extends

Mage_Adminhtml_Controller_Action{}

Helper File name: [Namehelper].php in directory app/code/[Codepool]/[Namespace]/[Module]/helper/ For example:

app/code/local/Masud/Part1/Helper/Data.php

app/code/local/Masud/Part1/Helper/Part1.php

Class name:

[NameSpace]_[Module]_Helper_[Filehelpername] For example:

Class Masud_part1_Helper_part1 extends Mage_Core_Helper_Abstract{}

Model: File name:

  • Model: [Nampodel].php in directory app/code/[Codepool]/[Namespace]/[Module]/Model/
  • Mysql4: [Nampodel].php in directory app/code/[Codepool]/[Namespace]/[Module]/Model/Mysql4
  • Collection: Collection.php in directory app/code/[Codepool]/[Namespace]/[Module]/Model/Mysql4/([Modelname]) Class name:
  • Model: [Namespace_Module]_Model_[Modelname] For example:

class Masud_part1_Model_part1 extends

Mage_Core_Model_Abstract{}

Mysql4: [Namespace_Module]_Model_Mysql4_[Modelname] For example:

class Masud_part1_Model_Mysql4_part1 extends

Mage_Core_Model_Mysql4_Abstract{}

Collection: [Namespace_Module]_Model_Mysql4__[Modelname]_Collection For example:

class Masud_part1_Model_Mysql4_part1_Collection extends

Mage_Core_Model_Mysql4_Collection_Abstract{}

SQL File name:

Installation file name: mysql4-install-[start-version].php in directory app/code/local/[NameSpace]/[Module]/sql/ For example:

app/code/local/Masud/part1/sql/mysql4-install-0.1.0.php

Upgrade file name: mysql4-upgrade-[old-version]-[new-version].php For example:

app/code/local/Masud/part1/sql/mysql4-upgrade-0.1.0-0.1.1.php

Block File name: [blockname].php in directories app/code/local/[NameSpace]/[Module]/Block/ and app/code/local/[NameSpace]/[Module]/Block/Adminhtml/ For example:

App/code/local/Masud/part1/Block/part1.php

App/code/local/Masud/part1/Block/ Adminhtml/part1.php

To create a Simple  "Hello World" module in Magento 1.9  is very easy. This would require a Namespace (say, "Masud") and ModuleName (say, "Hello"). Next follow these steps.

Where to put our module?

All Magento modules are located in /app/code/ folder and are split into three groups, called code pools: core, community, local.

/app/code
|- /core/
|- /community/
|- /local/

core – All original Magento modules are placed here. They provide base functionality of system and prototypes of blocks, models, helpers, controllers for further extending. The standard modules are: Mage_Core, Mage_Adminhtml, Mage_Catalog, Mage_Customer, Mage_Sales, Mage_Payment, Mage_Shipping etc. As you have probably noticed, all of them have prefix "Mage_", we will talk about this a bit later. Unless you are sure about what you are doing (and even afterwards) never edit the code in the core modules.

community – As you can see from the name, this group dedicated for modules that are provided by 3rd party developers (i.e. not by Magento team). When you install this modules throught built-in installer (Magento Connect Manager) all of them will be put into the folder /app/code/community/.

local – This folder is created empty on a Magento installation. Or it can be absent in last versions 1.7.x of CMS, in this case you’ll have to create it manually. Folder /app/code/local/ is used for your custom modules. All development, generally, is performed here.

MODULE NAMESPACE

The first directory we will create is a “namespace.” This can be called anything you like, but the convention is some form of the name of the company or module’s author. Magento uses “Mage” as its namespace.For this tutorial, we will use “Masud” as our namespace. So, create the directory app/code/local/Masud.

MODULE NAME

For the next directory, we will give our module a descriptive name. The module we are creating will show a Hello World Text, so a logical name would be Hello. Create the directory app/code/local/Masud/Hello.
We should now have the following directory structure for our module. These directory and file names are case-sensitive, so capitalize where appropriate.

Typical Magento module structure is represented by six main folders: etc, Block, Model, Helper, controllers, sql.

etc – This is the only required foder. All modules configuration xml files have to be placed here. Each module has required file config.xml.

Block – This folder is used for storing View classes (is we use terms of MVC architecture). Main goal of theese classes if to link Models data with template files from Magento themes.

Model – Models are used to work with different data. Generally, they are designed for connecting to the database and processing data in\out it.

Helper – Here we can store helper classes, which contain different utility methods, that can be called from any point of Magento (blocks, models, controllers, template files etc.). Helper methods a called this way Mage::helper(‘modulename/helpername’). Also each module has default helper class Helper/Data.php (in out case it will be /MyCustomModule/HelloWorld/Helper/Data). It can be called just by module name  – Mage::helper(‘modulename’).

controllers – This folder contains controller classes, that provide all business logics of out module.

sql – Files that are processed on module installation and upgrade, are located in this folder.

More detailed description of how to work with blocks, models, helpers I’ll try to provide in future posts.

Configuration and Naming in Magento

Unlike other platforms, Magento uses EAV database model. The advantage of this model is
its flexibility to use properties, which is very important to an e-commerce website.
In this section, we will learn about the database of some main modules of Magento including
Customer, Catalog and Sales.

Now let’s move to practice.

First steps

All further work assumes that you have installed and running Magento.
So, the first thing we need to do is disabling cache. Despite all pluses of caching on production servers,  it will disturb you during development, since it won’t let you see all changed data immediately and will slow down the development. You can disable cache by going to Admin Panel -> System -> Cache Management -> Select All -> Action: Disable -> Submit.
Now we will start creating our module.

Here is our directory structure for the module –

\app\etc\modules\Masud_Hello.xml

\app\code\local\Masud\Hello\etc\config.xml

\app\code\local\Masud\Hello\controllers\IndexController.php

\app\design\frontend\default\default\layout\hello.xml

\app\design\frontend\default\default\template\hello\hello.phtml

1. Activating Our Module

First we inform our Magento that our module exists, which we do by creating a new XML file in app/etc/modules. By convention we should give the file and module the same name. Let’s create app/etc/modules/Masud_Hello.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Masud_Hello>
      <active>true</active>
      <codePool>local</codePool>
    </Masud_Hello>
  </modules>
</config>

2. Configuring Routes

Next, we configure a route. A route will show a URL in an Action Controller and a method. Unlike other systems PHP MVC, you must explicitly define a route in the global config Magento. Let’s Create app/code/local/Masud/Hello/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <global>
    <modules>
      <masud_hello>
        <version>0.1.0</version>
      </masud_hello>
    </modules>
  </global>
  <frontend>
    <routers>
      <hello>
        <use>standard</use>
        <args>
          <module>Masud_Hello</module>
          <frontName>hello</frontName>
        </args>
      </hello>
    </routers>
	 <layout>
            <updates>
                <web>
                    <file>hello.xml</file>
                </web>
            </updates>
        </layout>
  </frontend>
</config>

What is a <frontend />   <admin />?

The <frontend /> tag refers to a Magento Frontend Area. The <admin> tag refers to a Magento BackendArea.

3. Create app/code/local/Masud/Hello/controllers/IndexController.php

<?php

class Masud_Hello_IndexController extends Mage_Core_Controller_Front_Action {

 public function indexAction() { 

  echo "Hello World at " . date('F j, Y');  

  $this->loadLayout();

  $this->renderLayout();

 }

}

 

4. Open http://localhost/magento/hello/index/ and watch your module in action on the top !

Magento Hello

5. Finally we need to show our own template. For showing your own template just Create \app\design\frontend\default\default\layout\hello.xml

<?xml version="1.0" ?>
<layout version="0.1.0">
    <hello_index_index>
        <reference name="content">
            <block type="core/template" name="hello" template="hello/hello.phtml"></block>
        </reference>
    </hello_index_index>
</layout>

6. Create \app\design\frontend\default\default\template\hello\hello.phtml

<h1>Hello World</h1>

7. Open http://localhost/magento/hello/index/ and watch your module in action on the Body !

Magento hello Module
Create Simple “HELLO WORLD” Extension with Magento 1.9
Magento Extension Development Part-1

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I graduated in 2009 with a bachelor’s degree in Engineering from State University Of Bangladesh, I’m also a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, CIDA and MAX Group where I worked on ERP software and web development., but now i’m a co-founder and Chief Executive Officer and Managing Director of TechBeeo Software Consultancy Services Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.
3 comments on “Magento Extension Development Part-1
  1. Hi Sir,

    Am currently using magento 1.9 version, followed your tutorial, I got result upto step 4. even after i followed all steps. and rwd is my magento default theme so i followed after 4th steps in rwd theme.

    Please help me in this.

  2. Excellent tutorial, but I can not get the template in the hello.phtml content. I have repeated it twice.
    Very grateful for your contribution.

  3. It works for me already. To display the template in the content.
    The path of step 6 should be: \ app \ design \ frontend \ base \ default \ template \ hello \ hello.phtml
    The path of step 5 should be: \ app \ design \ frontend \ base \ default \ layout \ hello.xml

Leave a Reply

Your email address will not be published. Required fields are marked *