How to Create a Magento 2 Module (A Developer’s Guide)

Contact Us

×

Get a Free Consultation

Creating a Magento 2 module is a fundamental process in extending the functionality of your e-commerce store. By establishing a structured module, crafting controllers to handle user interactions, and applying layout XML, blocks, and helpers for content presentation and utility functions, you can enhance your store’s features and design with flexibility and customization in mind. This modular approach empowers you to create tailored solutions that align with your business needs and customer expectations, making your Magento store a more powerful and engaging platform.

What is Module in Magento 2?

In Magento 2, a module is a self-contained unit of functionality that allows you to extend and customize the platform’s features and behavior. Modules encapsulate code, templates, configurations, and other resources related to a specific task or feature. They follow a structured directory hierarchy and can include controllers for handling user requests, layout XML files for page customization, blocks for managing content, and helpers for utility functions. Modules are integral to Magento’s modular architecture, enabling developers to add, remove, or modify functionality without affecting the core system. This modularity and flexibility make Magento 2 highly adaptable and customizable for e-commerce businesses.

Module Structure (Files and Folders)

Creating a module in Magento involves establishing a structured directory hierarchy and including essential configuration files. Below is the recommended directory structure for a Magento 2 module:

 

Module Structure

 

Let’s jump into the technical details about the explanation of each step in creating a Magento 2 module. 

Note:  We will just create the required files and folders that need to set up a basic Magento 2 module without jumping into more technical details regarding the above-mentioned files and folders.

1. Create the Module Directory:

Start by creating a directory for your module in the app/code directory of your Magento installation. The directory structure should be app/code/<Vendor>/<Module>, where <Vendor> is your company or personal namespace, and <Module> is the name of your module. This directory will contain all your module’s code and resources.

2. Define registration.php:

Inside your module directory, create a registration.php file. This file registers your module with Magento. It should contain PHP code like this:

 

Define registration

 

3. Set up module.xml:

Inside your module directory, create an etc directory and a module.xml file. This XML file defines your module and its version:

 

Set up module

 

4. Design a Controller:

Create a controller for your module to manage user requests. Inside your module directory, create a Controller directory and a subdirectory corresponding to your controller’s route. For example, Controller/Index/Index.php could be a simple controller:

 

Design a Controller

 

5. Craft routes.xml:

In your module’s etc directory, create a frontend/routes.xml file to define your custom route and link it to the controller. This XML file is essential for routing user requests to your controller:

 

Craft routes

 

Here is the detail about the xml sections:

  • <route id=”routename”>: This is the custom route ID that you define. It’s used to uniquely identify your route and can be anything you choose.
  • <frontName>frontname</frontName>: This is the part of the URL that will be associated with your custom route. For example, if the frontname is “myvendor,” you can access your controller actions via URLs like http://yourstore.com/myvender/controller/index.
  • <module name=”MyVendor_MyModule”>: This specifies the module that handles the requests for this route. Replace “MyVendor_MyModule” with your module’s actual name.

6. Add Layout XML:

In the view/frontend/layout directory of your module, you can create XML files to define the layout updates for your controller’s action. This is where you structure the page content and specify which blocks to include. For example, create 

Myvendor_mymodule_index_index.xml’:

 

Add Layout XML

 

7. Develop a Block:

Inside your module, create a Block directory and a PHP block class. This block class will be referenced in the layout XML. Here’s an example:

 

Develop a Block

 

8. Create a Template:

In the view/frontend/templates directory of your module, create a template file that corresponds to the template referenced in your layout XML. For example, create 

‘my_template.phtml’:

 

Create a Template

 

9. Implement a Helper:

Inside your module, create a Helper directory and a PHP helper class. This helper class can include utility functions to assist with various tasks within your module:

 

Implement a Helper

 

10. Enable and Test the Module:

In your Magento root directory, enable your module using the following command:

 

Enable and Test the Module

 

After enabling your module, you should clear the cache to see your changes:

 

Test the Module

 

After this, check the module output by visiting the given url:

http://yourstore.com/myvender/controller/index

 

Module Output

 

Conclusion:

This comprehensive article has illuminated the essential steps for crafting a module in Magento 2, equipping you with the fundamental knowledge required to embark on the development phase. In the subsequent articles, we will explore the intricate art of building different sections like blocks, helpers and crafting layout XML in Magento 2. These advanced insights will further elevate your proficiency in module development, enabling you to tailor your e-commerce store with finesse and precision.

About Author

Picture of Abdul Wahab

Abdul Wahab

Certified Adobe Commerce Developer with 10 plus years of development experience and 7+ e-Commerce specific experience. Having good expertise in web development and Other e-Commerce platforms, especially in Magento. The skill set includes Magento Customization, Theme Implementation, Third Party Integrations, Server Management, and Other platform-specific areas.

Table of Contents

Related Blogs

Magento Performance Optimization: 12 Proven Ways to Speed Up Your Store (2025)
Magento

Magento Performance Optimization: 12 Proven Ways to Speed Up Your Store (2025)

In today’s fast-paced e-commerce landscape, website speed directly impacts your bottom line—every second counts when customers browse your online store. If your Magento site takes too long to load, potential buyers will abandon their carts before completing purchases. Magento performance optimization has become essential for online retailers who want to stay competitive. Studies consistently show

Read More
Magento AI: Best Extensions, Tools & Trends to Grow Your Store in 2025
Magento

Magento AI: Best Extensions, Tools & Trends to Grow Your Store in 2025

The global AI market is set to reach $757.58 billion in 2025, with Generative AI spending projected to grow by 76.4%. For Magento (Adobe Commerce) stores processing over $155 billion in annual merchandise value, this isn’t just a trend—it’s a strategic imperative.  The question isn’t whether to adopt AI, but how to do it effectively.

Read More
Shopify Plus vs Magento 2: Complete 2025 Comparison Guide
Magento

Shopify Plus vs Magento 2: Complete 2025 Comparison Guide

Choosing between Shopify Plus vs Magento 2 can make or break your e-commerce success. Both platforms dominate the enterprise space, but they serve vastly different business needs. This guide breaks down the key differences to help you select the right platform for your online store in 2025. The e-commerce platform decision impacts everything: time to

Read More