Get a Free Quote

We respect your privacy

Creating a simple module in Magento

6:34 am

Creating a simple module in Magento

Magento is arguably one of the most popular open source ecommerce software around today. Magento comes in three different flavors:

Magento Web DEvelopment

  • Magento Go: This is the version that is targeted at small businesses
  • Magento Enterprise Edition: This version is targeted at small to medium businesses.
  • Magento: This version of Magento is free to download and is used by many web development firms to create a  e-commerce solution for their clients.

Magento is a very versatile platform with the basic installation providing loads of useful features. One of the main reasons that Magento has grown so popular is the fact it has lots of different features in built. Besides which it is also possible to install one of the many free and paid modules that are available online. But sometimes even that is not enough if you wish to customise the website to meet the client’s requirements. In such cases you need to make a module of our own and install it. Since Magento is built using PHP, you will need some programming knowledge and be aware about the PHP language to be able to customize the website and make a module.

Alakmalak is a company that offers the service of Magento Development. This is one company that has been involved in more than 2000 web development projects for clients from all around the world. Their skill full Magento Web Developers are more than capable to develop the right website for your business.

Creating the module – Declaration:

To start with create a new file by the name DP_FirstModule.xml and enter the code mentioned below in the file:

<?xml version=”1.0″?>
<config>
<modules>
<DP_FirstModule>
<active>true</active>
<codePool>local</codePool>
</DP_FirstModule>
</modules>
</config>

This file does the job declaring the module and letting your Magento website know that there is a new module. It needs to be uploaded to the your Magento installation in the following directory:

app/etc/modules/

Creating the module – Configuration:

Magento follows the popular pattern of MVC. For those who are not familiar with this abbreviation, MVC stands for Module, View, Controller. Thus each section of Magento, especially the functionality is divided into many different modules and each section consists of a file for the module code, the view code and the controller code. We will now create a controller class and name it IndexController.php with the following code:

class DP_FirstModule_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout(array(‘default’));
$this->renderLayout();
}
}

You will need to create several directories before uploading this file and adding it to your Magento installation. Follow the instructions mentioned here:

  • Traverse to the directory app/code/local with the help of a FTP software.
  • Make a new directory with the name ‘DP’
  • Make one more directory under this one called ‘Firstmodule’.
  • One more directory is required under this one called ‘controller’.
  • Thus the final result will be app/code/local/DP/Firstmodule/controller
  • Now upload the file you just created, IndexController.php to this directory.

The next step involves creating yet another file and this one is called FirstModule.php with the following code:

class DP_FirstModule_Block_FirstModule extends Mage_Core_Block_Template
{
// necessary methods
}

This file requires you to make a new directory, but just one this time. Use any software like FTP to drill down to the new directory that you have already created app/code/local/DP/FirstModule and create a new directory called ‘Block’ and then upload the file you just created there. Thus after you have done so you will have something that looks like this:

app/code/local/DP/FirstModule/Block/FirstModule.php

There is one more file that is required to completed the configuration of the new Magento module. This is an XML file called config.xml with the following code:

<?xml version=”1.0″?>
<config>
<global>
<modules>
<dp_firstmodule>
<version>0.1.0</version>
</ dp_firstmodule >
</modules>
<blocks>
<firstmodule>
<rewrite>
<firstmodule>DP_FirstModule_Block_FirstModule</firstmodule>
</rewrite>
</firstmodule>
</blocks>

</global>
<frontend>
<routers>
<firstmodule>
<use>standard</use>
<args>
<module>DP_FirstModule</module>
<frontName>firstmodule</frontName>
</args>
</firstmodule>
</routers>
<layout>
<updates>
<firstmodle>
<file>firstmodule.xml</file>
</firstmodule>
</updates>
</layout>
</frontend>
</config>

Creating Module – Template:

Custom Module in Magento

The last set of files that needs to be created is firstmodule.xml and firstmodule.phtml. Since this is a template and to do with the layout of the module and not the code it is located in a different section of the codebase. Here too you will need to make a few new directories by following the instructions mentioned below:

  • Traverse to the directory app/design/frontend directory.
  • Create a new directory by the name DP here.
  • Create one more directory under this one called default.
  • Create yet another directory under this one called layout..
  • Now upload the file firstmodule.xml here.
  • Thus the final structure will look like this: app/design/frontend/DP/default/layout/firstmodule.xml
  • In the case where you are using the default design packages, the file you just created will be need to be uploaded to a different location like app/design/frontend/default/default/layout/firstmodule.xml

The code for the file firstmodule.xml is listed below:

<?xml version=”1.0″?>
<layout version=”0.1.0″>
<fistmodule_index_index>
<reference name=”root”>
<action method=”setTemplate”><template>page/1column.phtml</template></action>
</reference>
<reference name=”content”>
<block type=”firstmodule/firstmodle” name=”hello” template=”firstmodule/firstmodule.phtml”/>
</reference>
</firstmodule_index_index>
</layout>

The code for the file firstmodule.phtml is listed below:

<h2>This is my first module, made for Magento.</h2>

This file needs to be uploaded to the directory app/desgin/frontend/DP/default/template/firstmodule/firstmodule/phtml

Thus as you should have guessed by now you will first need to drill down to the directory app/design/frontend/DP/default and then create two new directories template and firstmodule prior to uploading the file.

This is the end of the short tutorial and we have successfully created a module for out Magento Website. But how do we test it. You can do so by simply loading the module in your browser. For example I have been working on a Magneto project installed on my local machine in the directory magento, hence the URL for me will be http://localhost/magento/index.php/firstmodule

If you have installed your Magento files on a server online then you will need to replace the text like ‘localhost/magento’ with the name of your domain.

 

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...Loading...

Comments are closed.