Quantcast
Channel: JTB Productions
Viewing all articles
Browse latest Browse all 49

Magento Custom Module Development

$
0
0
Magento custom module development is a core part of any Magneto development or Magneto project, because at any stage you may want to integrate your own functionality/module in your existing Magneto project.
In this series, I am going to cover Magneto custom module development in detail.

Throughout whole series i am referring Magneto community version 1.7, though custom
module structure is same in all versions of Magneto but its worth to clear version. Before going to start actual module development let’s quickly understand the basic structure of Magneto. Whenever you install a fresh Magneto, you will come up with the following Magneto directory structure.

Introduction to Magento MVC Structure

Like any other major frameworks (Joomla, CakePHP, CodeIgniter, and so on) Magneto also follow MVC based architecture which is little bit different than core PHP MVC architecture. Here, I am explaining the difference in Magento architecture by comparing it with simple PHP MVC architecture.

PHP MVC architecture

In typical MVC pattern the flow of the application is something like this:

  1. There is main entry point – index.php – from where the entire app routing mechanism is determined.
  2. Based on this routing mechanism and requested URL pattern, the app will call the appropriate controller.
  3. Controller call appropriate view files.
  4. View files collect the data from model files and display the data visually.

Magento MVC architecture

Magento MVC architecture contains some more layers to mvc pattern, base on that the flow of the magento application is something like this

  1. There is main entry point (index.php) from where the whole app will be initialized.
  2. Base on the requested URL appropriate controller will be called.
  3. Controller defines the pages and load the layout files for those pages.
  4. Layout files tells the controllers which block files to use.
  5. Block files collect the data from models and helpers files and pass it to templates files.
  6. Templates files receive data and render html.

At first stage its difficult to understand the whole architecture of magento MVC as its contains some extra layers. Let’s develop one custom “Hello World” module, it will more clear the flow of mvc pattern in magento.

Before Starting With Modules

  • I am assuming that you already have a working copy of magento with version 1.7 or 1.7+ (or else as version does not matter at this stage)
  • Disable the cache. To Disable the cache Go to Magento Admin Panel > System > Cache Management > Select all cache type from left side checkboxes > Select Action: disable from right top drop down > click Submit.

The Structure of a Magento Module

Code Pools

Magento contains three type of code pools where the all custom and core modules of Magento are resides.

Here are the three:

  1. Core: Core pool contains all the core modules which are by default comes with magento installation. These modules are written by Magento developers. Its recommended not to modified this modules. Because whenever you will upgrade your magento version all the core modules will going to overwrite and your modifications will be lost.
  2. Community: Community pools contains all the modules (custom modules) which are developed by third party (to plugged more functionalities) or installed through Magento Connect These modules generally extended core modules files and fit their own functionality in it or contains completely separate functionality to use anywhere in magento.
  3. Local: Local pools contains all the module (custom modules) which are going to used in particular platform/project but do not going to release in Magento Connect

Here we have two choice of pools either Community or Local, But we are going to make our development in Local pool as its specific to project. (You can also make your module in community pool there is no strict restriction.)

Structure

Magento Module consists of following components:

  • Block: Contains functions which are use to display data in templates.
  • Models: Contains business logic of module.
  • Resource Models: Contains functions which are use for database interaction.
  • Controllers: Base on requested URL defines pages layout and blocks files.
  • etc: Contains configuration files in XML formats which tells magento how many files modules have and how the module interacts.
  • Helpers: Contains functions which are use for define common business logic (image resize, validation). These functions can use anywhere across app.
  • sql: Contains SQL scripts for create, modify or delete SQL tables.

Module Naming

We need to give the name of the our module. Generally Magento module name made of two part _. Best practice to give magento module name is choose as an author or a company name and as a actual module name.

Base on these naming convention i am giving our module Chiragdodia_Mymodule name. We will reference this name in whole series.

Code Set Up and Configuration

Lets create the directories base on above structure. Go to your Magento Installed Directory > app > code > local and create the directories as shown in below.

Next we will configure and activate our module by creating config file “Chiragdodia_Mymodule.xml” in app > etc > modules directory. This directory contains config files of all modules.

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

This file will tell magento about location of module. Here we have specified the code pool that is “local” because we will going to make our module in local pool. In an active tag, we have specified true that means our module is enable. If up to this everything is correct, you will find your module in Magento Admin Panel > System > Configuration > Advanced > Advanced > Disable Modules Output list. From here you can enable/disable your module.

Getting Started: Development

Next we will create our module configuration file, this file will tell magento all about our module that is how many files our module contains, which types of files (models, helpers, database classes) etc.

Go to app > code > local > Chiragdodia > Mymodule > etc and create config.xml file which contains following content

<?xml version="1.0"?>
<config>
    <modules>
        <Chiragdodia_Mymodule>
            <version>0.1.0</version>    <!-- Version number of your module -->
        </Chiragdodia_Mymodule>
    </modules>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>Chiragdodia_Mymodule</module>
                    <frontName>mymodule</frontName>
                </args>
            </mymodule>
        </routers>
    </frontend>
</config>

Let’s go through line by line to understand each tag. Here, the first tag is that contains the name (Namespace_module) and version of our module. Version number is very important while update your module.

tag will tell Magento about the controller dispatched. In side tag we have defined which tell Magento about how the Magento access our controller by routing mechanism. In tag we have defined module name in tag and frontend name in . By using frontend name we can access our module in frontend like yoursitename.com/index.php/mymodule/index.

By calling yoursitename.com/index.php/mymodule or yoursitename.com/index.php/mymodule/index Magento will look for index action of your module’s controller file. So now we need to create our controller file.

Go to app > code > local > Chiragdodia > Mymodule > controllers and create file IndexController.php with following content.

Note: Each file’s name and class names are Case Sensitive in Magento because base on the class name Magento is looking for particular class file and include it. So it’s very important that you are taking care in name while creating any file and class.

<?php
class Chiragdodia_Mymodule_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "Hello tuts+ World";
    }
}

Now open URL yoursite.com/index.php/mymodule/index it will print “Hello tuts+ World”. Awesome – we’re finally done with our first hello world module.

Controller Dispatch

Here we have extend the class Mage_Core_Controller_Front_Action that contains all the methods which are using in routing of url. Magento class name reflects the location of class file. So the class Mage_Core_Controller_Front_Action reside in location Mage > Core > Controller > Front > Action.php

See the class name of our controller that is Chiragdodia_Mymodule_IndexController. Magento controller to be named in such a way that it reflects (tag)_(Action Controllername)(keyword Controller).

  • tag = Chiragdodia_Mymodule (we have defined this tag in config.xml)
  • Action Controller name = Index
  • Action controller follow by Controller keyword

Base on this pattern the name of our controller is Chiragdodia_Mymodule_IndexController

Now see the URL pattern which is follow the below route pattern
yoursite.com/index.php/frontendname/actionControllername/actionmethod

  • frontendname = mymodule
  • actionControllername = Index
  • actionmethodname = Index

Base on this URL pattern our module url is yoursite.com/index.php/mymodule/index/index. You can access using yoursite.com/index.php/mymodule also because whenever you are not specified actionController and actionmethod name Magento by default load IndexController with IndexAction.

Now let’s create one more action that is testAction so the code is like following

<?php
class Chiragdodia_Mymodule_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "Hello tuts+ World";
    }
    public function testAction()
    {
        echo "test action";
    }
}

We can access the testAction using URL yoursite.com/index.php/mymodule/index/test. As explained earlier here

  • frontendname = mymodule
  • actionControllername = Index
  • actionmethodname = test

This is how the controller works in Magento, as a first sight its hard to get all things at once so here I have attached the whole module code, you can try by implementing its with your own name.

What’s Next?

In next part, we will going to fill some layouts in our module by creating layouts and blocks files. We will understand how the layouts files works in Magento and what is the use of blocks files in Magento.

Until then create your own module and let me know of any doubts you may stumble across!

Source: nettuts


Viewing all articles
Browse latest Browse all 49

Trending Articles