Milind Daraniya

How to Create a Custom Module in Magento 2.4?

Published June 18th, 2023 3 min read

Today, We will learn how to create custom module in magento 2.4.x. 

As you know, the module is a directory that contains blocks, controllers, models, helperetc - that are related to a specific business feature.

So are you ready to create our first Magento2 module !!!!

We are going to implement this in following steps:

  • Step 1: Create the folder of Hello World module
  • Step 2: Create etc/module.xml file
  • Step 3: Create etc/registration.php file
  • Step 4: Enable the module

Step 1: Create the folder of Hello World module

Create the following folders in the magento project root directory (ex. – C:\xampp\htdocs\magento2):

  • app/code/Milind
  • app/code/Milind/HelloWorld

The Milind folder is the module’s VendorName, and HelloWorld is the ModuleName.

Step 2: Create etc/module.xml file.

It is necessary to create etc folder and add the module.xml file in it

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

Contents would be:

<?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="Milind_HelloWorld" setup_version="1.0.0">
    </module>
</config>

Step 3: Create etc/registration.php file

create a registration.php file in the app/code/Milind/HelloWorld/registration.php

Contents would be:

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

Step 4: Enable the module

Now its time to enable our moule. Lets check the status of our module by typing below command in the magento project root directory :

php bin/magento module:status

You should see the module is disabled now:

List of disabled modules: Milind_HelloWorld

Follow exact guide to enable the module right now, let run the command as:

php bin/magento module:enable Milind_HelloWorld

You have enabled the module first time, so you have to upgrade the database of the module by using this command line:

php bin/magento setup:upgrade && php bin/magento se:s:d -f

And here you go, these steps can lead you to successfully create a new custom module in Magento 2.

Thanks