Magento create your first Custom Module

There are lot of paid and free extensions/modules are available online, but most of the time we need to implement some custom functionalities and for this we should need to understand how the modules are created in magento and their basic workflow and structure.

Before we start further, let’s have a look on magento basic structure.

=> webroot [your magento project root directory]

=> app [contains code pools, design, configuration, localization and Mage.php files]

=> code [contains the code files, core/community/local]

=> community [third party modules]

=> core [magento core modules]

=> local [local modules]

=> design [contains modules templates and layout files]

=> adminhtml [contains the templates and layout of admin area]

=> frontend [contains the templates and layout of front area]

=> install [contains installation files]

=> etc [contains the system and modules configuration files]

=> local.xml
=> config.xml
=> modules [module configuration files]

=> locale [contains the localization files like translation and email templates]

=> Mage.php [main entry point of magento framework]

=> js [javascript library files]

=> lib [external libraries include varien, zend framework, etc.]

=> media [contains product, cms images, etc]

=> skin [contains the css and js file based on the theme]

=> var [contains the system generated directories like cache, report, sessions etc]

=> cron.sh [cron to make task automatically]

=> index.php [main entry point for php application]

So let’s have a brief overview of all the code pools.

  • Core  The core directory contains the core modules which come with magento community/enterprise edition release. It is strictly not recommended to change core files because when we need to upgrade our magento all our core changes will be overwritten.
  • Community  The community codepool contains all the third party extensions and modules either downloaded from the magento connect store or installed manually. You should place modules code in this directory if you are planning to upload the module to magento connect store.
  • Local – We put all our projects’ specific modules into the local directory, create one if it is not already present in the code directory.
  • Block – Block files are used to fetch the data and display it on the template files.
  • Model  The model files contains the business logic
  • Controllers  Controllers handles the incoming request and renders the pages according to the routes defined in the layout files.
  • Helper – Helper files, as the name suggest contains, the common code required in the whole module like (validations, image processing, etc.).
  • Etc – etc directory contains the module configuration files which are in xml format. Basically there are two main files. config.xml and system.xml.
  • Sql – sql directory contains the database query files which are in need for that module.

So let’s create your first “HelloWorld” Module.

First thing to remember before creating Module/ Disable Magento Cache:

This is one of the first lessons a Magento developer should learn: disable the cache! You can do this by going to Admin Panel-> System -> Cache Management -> Select All -> Actions: Disable -> Submit.

Follow the convention:

Always follow the naming convention for you module name, functions and the variable names.

so your module name should be like Yourcompany_Module name, and in our case it will be like Magehelpers_Helloworld

Our Module Directory structure:

Create directory structure like app/code/local/Magehelpers

Step 1: Activating your module: Go to the app/etc/modules directory and create the Magehelpers_Helloworld.xml file and paste the below code in the file:

Now we have a just created our module. To check it out, log into the Magento Admin Panel and navigate to System > Configuration > Advanced >Advanced and view the Disable Module Output listing. We should see here our module Magehelpers_Helloworld module. If not, carefully run through the steps again and diagnose the mistakes.

Step 2: Now as we can see our module in the backend and it is enable, next we will start creating the required files and directories one by one.

So first create Magehelpers directory in local code pools. app/code/local/Magehelpers

and then create Helloworld directory inside your Magehelpers directory.

The structure would be like this. app/code/local/Magehelpers/Helloworld

Step 3: Now create etc directory inside Helloworld and create module config.xml file inside it like app/code/local/Magehelpers/Helloworld and create XML file with name config.xml under the etc directory. This file will tell magento everything about the structure of our module.
Put the below code in the config.xml file:

Where:

<config>/config> is main configuration tag which contains all other tags.

<modules>/modules> tag, is where we define our module name and version which is used for update and install script.

<frontend>/frontend> tag, frontend meain, it is scop is for frontend only and here under <routers>/routers> we have defined routers front-name “helloworld”, and it contains the <helloworld>/helloworld> tag which identifies our module in the magento system.

<use>/use> will tell magento which router we are using, it can be standard, admin or default.

<args> tag contains the information about our module and the value in the <frontName> tag will be used to access our module on frontend.

So we can now access our module like: http://example.com/index.php/helloworld/

Step 4 : Create controller file inside app/code/local/Magehelpers/Helloworld/controllers/
Create IndexController.php file inside app/code/local/Magehelpers/Helloworld/controllers/ and place below code inside it.
Create File: app/code/local/Magehelpers/Helloworld/controllers/IndexController.php

Here for the controller for frontend, we need to extend it from Mage_Core_Controller_Front_Action “Magento core base action”
Now we can access our module either http://example.com/index.php/helloworld/ — or http://example.com/index.php/helloworld/index/index/ —here we don’t need to add index/index after helloworld as magento automaticallly load indexAction of indexController
and for our test action, we can access http://example.com/index.php/helloworld/index/test/

Step 5: Your module is ready, let’s look how we can create our own layout for our custom module:

Create your block file inside: app/code/local/Magehelpers/Helloworld/Block/Helloworld.php and place below code inside it.

We add all the code and functions related to our layout in our block.

Step 6: Create your layout file helloworld.xml inside app/design/frontend/default/default/layout/helloworld.xml and place below code inside it.

 

Here: helloworld_index_index => helloworld is our module’s “firsName”

  • helloworld_index => second index represents our moudle controller name i-e IndexController
  • helloworld_index_index => Third index tells to use index method inside indexController
  • Block Type “helloworld/helloworld” is our block Magehelpers_Helloworld_Block_Helloworld

Step 7: Create your template file helloworld.phtml inside app/design/frontend/default/default/template/helloworld/helloworld.phtml and place below code inside it.

 

Congratulation you have successfully created your first custom module, now you can access your module anywhere.

Just place this code in your cms page: cms->pages->homepage

{{block type=”helloworld/helloworld” name=”helloworld” template=”helloworld/helloworld.phtml” }} in content area.

You can also access your block by placing this in your xml file like: page.xml

<block type=”helloworld/helloworld” name=”helloworld” template=”helloworld/helloworld.phtml” />

Thank you for reading this blog, add your comment below if you have any query.

Leave a Reply

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

7 + six =