Cygnite PHP Framework

How to create custom module in Magento?


Introduction:



My previous article was “How to install custom theme in Magento”.. In this article I will talk about magento custom module.

Though Magento community provides you lot of modules to download or install from various third party, but what if those built in modules or third party modules doesn’t fulfill your current requirement? In such case developers can create their own custom module by understanding the Magento architecture and building it to enhance existing functionalities.

Many developers get stuck while creating their custom module into magento, in this tutorial I will show you “how to create your own custom module in Magento” with some simple steps. It is not possible to explain everything about Magento custom module creation, so in this article I will just explain about simple custom module then next series of tutorial for the rest.

Where to start with?

I assume you have installed Magento already into your system if not you may follow Magento step by step installation tutorial.

All magento modules are located into /app/code/ directory. This directory consists of -

a. code:

- core
- community
- local

i. core: Consists of all core classes of Magento.
ii. community: Consists of community modules of Magento.
iii. local: You may not find this directory on installation of latest Magento. You can manually create it, where our custom module will be placed.

We will call above three directories together as “code pool”.

Another two main directory we should know are: “design” and “etc”

b. design: This folder consists of all your layout and view files.

c. etc: Consists of module configuration files.

Getting Started with Magento Module Development:

Step1: Module Naming

We need to give your custom module name as per the Magento standard. Your module name should be look as below.


  <Namespace>_<Module>

For example I am creating custom module

  <Cygnite>_<HelloWorld> 

Step2: Custom Module Structure:

Magento custom module consists of below.

Block: Consists of classes that are used to display data in view template file.

controllers: This folder consists of controller classes. Controllers are request handler (when user request url), used to define page layout, block etc.

etc: Consists of module configurations files. It has config.xml file. It tells magento how many files module has and module version etc.

Helper: Consist of helper classes. Helpers are act as utility class with functions, which can be used anywhere in the application.

Model: Consists of business logic of the module. Generally models are to interact with the database and processing data.

sql: This folder consists of sql script file, which is used to create, update or delete tables.

Now go to app/code/local/Cygnite/HelloWorld/controllers/ and create controller class called IndexController.php with below content.



class Cygnite_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
       echo "Hello 'appsntech.com' World!!";
    }
}


Step3: Creating Frontend Module and Routing Mechanism:

Now go to etc folder inside the same Cygnite > HelloWorld module and create a file called config.xml and paste below contents inside.


<?xml version="1.0"?>
<config>
    <modules>
        <Cygnite_HelloWorld>
            <version>0.1.0</version>
        </Cygnite_HelloWorld>
    </modules>
	
    <frontend>
        <routers>
            <helloworld>
                <use>standard</use>
                <args>
                    <module>Cygnite_HelloWorld</module>
                    <frontName>cygnite</frontName>
                </args>
            </helloworld>
        </routers>        
    </frontend>     
</config>

In this above xml file in the tag we are defining module name and version. Inside tag we have where we will define our routing to our module, by this magento will understand how to access our module via url. Inside the tag will define the front name by which we can access the controller.

Now we can access the IndexController using below url.


    http://localhost/index.php/cygnite/index

Before that we need to active our custom module.

Step4: Configure and Activating Custom Module:

We need to active our custom module in order to access it. We will create a config file inside app/etc/modules/ folder by naming it “Cygnite_HelloWorld.xml” and paste below code.


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

This configuration file will tell magento where our module is located. In the above tag used to activate or deactivate the module. We have specified true inside tag, meaning that our module is activated. To confirm you can also login into admin panel and go to System -> Advance -> Cygnite_HelloWorld and enable or disable it.

Congrats! We are done with. Now go to url http://localhost/index.php/cygnite/index and it will print “Hello appsntech.com World!!”. We are done with our first hello world custom module.

Conclusion:

Hope you found this article helpful for step by step magento custom module creation. If you are also interested to write tutorial for us you can post me. If this tutorial helps you then please don't forget to like, share with friends, or leave your comments below to share some love us back.

Keep visiting for upcoming posts. Have a nice day.

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2013 www.appsntech.com. All rights reserved. Powered By- Sanjoy Dey Productions