How to Create a Custom Module in Magento 2?

Module development is a structural element of Magento 2. Because the whole Magento system is built of these various Magento 2 modules. Magento 2 allows its users to create a custom module based on their business requirements. But, the Magento customer module includes a lot of things such as models, controllers, blocks, helpers, and so on depending on particular store requirements. So let's check how to create a custom module in Magento 2.

Magento always comes up with a variety of features for eCommerce store owners and  Magento users. Moreover, using Magento for your online business is worth it. We all know that Magento is based on PHP language. So while creating a module we need to keep in mind the MVC structure. That will help a lot in Magento module development.

How to create a custom module in Magento 2?

While Magento 2 extension development, we will work mostly in the app/code directory of Magento installation. Here is the format that we need to follow: app/code/<Vendor>/<ModuleName>. In this tutorial, we will create a Hello World module in Magento 2. Here is the step-by-step guide for how to create a custom module in Magento 2.

1. Create a module folder

In Magento 2 module creation, there might 2 possible ways to create a module folder. The first one is the app/code folder and the second is the vendor folder. Choose this folder based on where the Magento has been installed.

Let me help you with choosing a folder. The folder selection can be based on the development purpose that if you are building a module for a particular project then it is ideal to select the app/code folder and commit to the project’s repository.

However, if you are building a module for reuse, then it will be best to use composer to create it and put your module in the vendor/<YOUR_VENDOR>/module-something folder.

Moreover, all individual module names consist of two parts in Magento 2. Vendor and Module. You need to define vendor and module names. Because all the modules are grouped into the vendors. Let's set the name "Milople" as a vendor and "Hello World" as a module.

Now it's the time to create a folder. So, create a folder app/code/Milople and place the Hello World module inside the Milople folder. Here is the code, if you are using the command line.

cd to the root folder
mkdir app/code/Milople
mkdir app/code/Milople/HelloWorld

Do not forget to give permission to create a file and folder in your installation.

2. Create etc/module.xml file.

Now, create an etc folder and add a module.xml file.

app/code/Milople/HelloWorld/etc/module.xml

Then, add the following code to the file.

<?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="Mailople_HelloWorld" setup_version="0.0.1">
    </module>
</config>

3. Create a registration.php file

Regustration.php informs Magento on how to locate the module. Therefore, it is important to create a registration.php file.

Now, create a file app/code/Milople/HelloWorld/registration.php.

Then, copy-paste the following code to the file.

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

4. Enable the Module

Now that we have created a module after completing the third step, now it is time to activate the module. To enable the module you run the below command.

php bin/magento setup:upgrade

Moreover, after running the setup: upgrade command, check the website. For that, you need to check the file app/etc/config.php. It displays all the module lists that are active.

NOTE - Do not change this list manually.

$ grep Milople_HelloWorld app/etc/config.php

That's all!

You just need to follow the above-listed steps to successfully create a new custom module in Magneto 2. So, we hope that our tutorial on how to create a custom module in Magento 2 is helpful and effective for you. Moreover, if you face any issues while creating a custom module in Magento 2, feel free to contact us. The comment section is all yours. We will be more than happy to serve you.

Also, check out our new Magento 2 Extensions

3D Product Viewer for Magento 2

Magento 2 Thank You Page

GeoIP redirect for Magento 2

Order Notification for Magento 2 store owners

Hope you find these extensions useful for your eCommerce store.

Happy Coding :)