Loading...
How to create a module in Magento 2: Sample Module A Step-by-Step Guide

How to create a module in Magento 2: Sample Module A Step-by-Step Guide

Introduction

A module in Magento is like a separate piece that adds or changes things in the Magento system. It's like a building block that developers use to customize or improve the features of a Magento online store to fit specific business requirements.

Modules in Magento 2 are added to your store via a component library designed to offer several features and functionalities, such as payment gateway integration or user role management. You can choose from several modules available, including dynamic blocks that allow you to create customized blocks on the fly, custom HTML pages and layout blocks, contact forms and SEO-related components.

How to create a module in Magento 2 - Sample module step by step

This tutorial will guide you in creating a module in Magento 2. The app/code folder in Magento 2 is the best place to start building your module.

To create the module, go through the steps described below.

#1: You must switch your Magento mode to "developer" You must run the following command in your terminal or console.

php bin/magento deploy:mode:set developer

#2: Create a folder with your module name in the app/code.

app/code/Namespace/Modulename

#3: Now you need to create the required file module.xml inside the etc. of Model Directory.

app/code/Namespace/Modulename/etc/module.xml

content of the file

xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Namespace_Modulename" setup_version="1.0.0">
  </module>
</config>

#4: We need to register your module and create registration.php in the Module Directory.

content of the file

php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Namespace_Modulename',
__DIR__
);

#5: Use the command provided in the terminal or console to enable the module.

php bin/magento module:enable Namespace_Modulename

an alternate way to enable the module is to go to the following file

app/etc/config.php

and add the following info on the config.php file

‘Namespace_Modulename’ => 1

 

#6: Now, you need to run the final command to finish the sample module of Magento 2.

php bin/magento setup:upgrade

after running this command, you do not need to deploy the static content because you are in developer mode.

 

Your module is ready to create BLOCK, CONTROLLER, MODULE, PLUGIN, OBSERVER, HELPER, etc., inside it.

Modules in Magento 2 allow developers to create custom features and functionalities to improve performance and user experience and stay ahead of the competition.


Contact us:+91 8128405131

Email send us at hello@elightwalk.com

Jayram Prajapati
Full Stack Developer

Jayram Prajapati brings expertise and innovation to every project he takes on. His collaborative communication style, coupled with a receptiveness to new ideas, consistently leads to successful project outcomes.

Most Visited Blog

Blog
Unlock The Power Of Plugins In Magento 2: A Comprehensive Guide

Get started on utilizing Magento 2 plugins with our comprehensive tutorial. Master the art of plugin building and integration to improve the functionality and customization of your e-commerce shop.

Read More
Blog
The Ultimate Guide to Creating the Best Grid in Magento of 2022

Dive into the guide to creating an excellent Magento grid in 2022. Discover professional insights, strategies, and tactics for improving the grid design and functioning of an e-commerce platform for a better user experience.

Read More
Blog
Password Option Change using the command!

With a single command, you can improve the security of your website! Explore our step-by-step guide to password options and creating a robust and unique authentication method for online platform.

Read More