Pages

Monday, August 24, 2015

Magento Custom Module Development

Magento custom module development is a core part of any Magento development or Magento project, because at any stage you may want to integrate your own functionality/module in your existing Magento project. 

In this series, I am going to cover the details of Magento custom module development.

Throughout this series, I'm referring Magento Community Edition 1.7, though custom module structures are the same in all versions of Magento. Before going we're going to start actual module development, let's quickly understand the basic structure of Magento. 

Whenever you install a fresh Magento, you will notice the following Magento directory structure:



Like any other major frameworks such as Joomla, CakePHP, CodeIgniter, etc., Magento also follows the MVC-based architecture though this is little bit different than core PHP MVC architecture. Here, I'll explain the difference in Magento architecture by comparing it with simple PHP MVC architecture.
In the 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. The controller then calls the appropriate views.
  4. Finally, the view files collect the data from model files and display the data.
Magento's MVC architecture adds a few layers to the MVC pattern, but the basic flow of control of an application is 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.
Initially, this may be difficult to understand since it contains a few extra layers. To get more familiar with the flow of control, let's develop a custom "Hello World" module.
  • 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.
Magento contains three type of code pools where the all custom and core modules of Magento are resides. 
  1. Core pools contain all the core modules which are by default comes with a Magento installation. These modules are written by Magento developers. It's recommended not to modify these modules because whenever you will upgrade your Magento installation, all the core modules will be overwritten and your modifications will be lost.
  2. Community pools contain all the modules - that is, custom modules - that are developed by third-party programmers to be installed through Magento Connect. These modules generally extend core modules and offer their own functionality that can often be used anywhere in Magento.
  3. Local pools contain all the custom module that are going to be used for a particular project but are not readled in Magento Connect 
Thus, we have two choice of pools: Community or Local. Since we are working on our own project, we are going to use a local pool, though there's no restriction on using the community pool, either.
Structure
Magento modules consist of the following components:
  • Blocks contain functions that are used to display data in templates.
  • Models contain the business logic of modules.
  • Resource Models contains functions that are used for database interaction.
  • Controllers defines page layout and blocks files and are loaded when a URL is requested.
  • etc contains configuration files in XML formats which tells Magento how many files modules have and how the module interacts.
  • Helpers contain functions that are used for defining common business logic (such as image resize, validation). These functions can used anywhere across the Magento application
  • sql contains SQL scripts to create, modify, or delete SQL tables.
We need to give a name to our module. Generally, Magento module names are made of two parts: <Namespace>_<Module>. The best practice to give a Magento module a name is choose <Namespace> as an author or a company name and<Module> as a actual module name.
Based on these naming convention, I am giving our module the Chiragdodia_Mymodule name. We will reference this name throughout this series.
 Lets create the directories base on above structure. Go to your Magento installation direction then navigate to app/code/local  and create the directories as shown below.


Next, we will configure and activate our module by creating config fileChiragdodia_Mymodule.xml in the app/etc/modules directoryThis directory contains config files for all modules.
This file will tell Magento about the location of our module. In the active tag, we have specified true to enable our module. If everything is correct thus far, then you will find your module in the Magento Admin Panel  > System > Configuration > Advanced > Advanced  > Disable Modules Output list. From here you can enable and disable your module.

No comments:

Post a Comment