Magento eCommerce Blog » Podcast Feed

Magento eCommerce Blog » Podcast Feed


Understanding Magento Module Structure and Code Execution

February 16, 2014

Magento is a typical object oriented PHP framework which is used to develop web applications that convert to form a full-fledged, fully functional and dynamic eCommerce web shopping cart. The powerful features provided by Magento range from placing order, adding items to entering the payment gateway and completing the payment schedule.
In case of Magento extension, you create a unique code which will help extend some functionality without interfering with the core codes. Let’s understand how this core and extension codes work simultaneously and how the Magento module structure looks like.
Magento Extensions: Basics
Before moving on to understand the module structure in Magento, it is essential to understand the three kinds of Magento extensions that are present.

The Core Extension: This is particularly developed by Magento. It is available by default. You generally don’t make any sort of changes in here.
The Community Extension: Magento community is where the open source Magento source is available. Here the community developers come up with various extensions that are available for free.
The Local Extension: There is a certain need that you can fulfill using an extension. This is where the local extension comes to use. You will see that local extensions are either developed by third party or by you in order to complete a specific task.

Magento Module Structure
The module structure for Magento begins with Namespace. This is the Magento directory that is present above your extension. Let’s say it is the category of extension or in other words the main class that contains the extension. So, in a way the module structure begins with NameSpace. Most people use their company name in place of the Namespace to define the category of extension.
Now the MVC structure of Magento has a few predefined blocks that you need to understand. It helps to understand why each block is present and how it is useful to you.

Controller: In any normal MVC structure, the controller is a class that controls the overall logical flow and presentation of the Magento layout and functioning. It is a class which you need to create using the PHP extension. Generally the name format is indexcontroller.php. Controller in general is used as the prefix in most cases.
Action: Let’s say action is the function of a controller in a standard MVC structure. Any action that is performed by the module or the extension when translated to the outside would be considered as the function of the controller.
Block: This is what you view when you are viewing a typical Magento store. Blocks will coordinate with the models to give out a view format for the store.
Helper: All the utility functions that are typically needed by block, view and other extensions would be stored in this module called helper. It can be called anywhere at any point by the system.
Model: This is the main folder in your Magento coding. This is where your business logic is stored and handled. Object Relation Mapper is utilized to build Magento codes. It loads the database field using PHP magic function.
SQL: If you are planning to install or upgrade a particular model, SQL custom handle comes to your rescue. You will need to create mysql folder while installing your module or extension.

The Flow of Code Execution
Before you begin understanding how the code is executed, it is very important to understand how each request Magento receives is being served from its quarters. This will help you understand how Magento works.
There is a .htaccess file in the root directory. This is where the routing of every request is properly defined. A few rewrite rule help establish the right routing of the requests. In case of the rewrite command, there would be an explanation as to why it has been used and how it will be used further.
Let’s say a request comes which has no file or directory support, then the rewrite command will route it forward. In case, your command defines a directory name which cannot be id