How to create custom module in magento 2

Hello Friends, today we will discuss about how to create custom module in magento 2.

Create Custom Module in Magento 2
Create Custom Module in Magento 2

In Magento2 there is a drastic change in the structure of the code. All the code pools are removed and also the Skin directory. There are many changes related to the theme folder as well. Now we will follow this steps to create a custom module which work on Magento 2.

Create Custom module in Magento 2

To create custom module, you need to complete the following steps:

  • Step 1: Create the folder of CustomModule 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 CustomModule module

Name of the module is defined as VendorName_ModuleName. First part is name of the vendor and last part is name of the module: For example: W3codezone_CustomModule. Focus on following guide to create the folders:

  app/code/W3codezone/CustomModule

Step 2: Create etc/module.xml file.

Then, it is necessary to create etc folder and add the module.xml file

app/code/W3codezone/CustomModule/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="W3codezone_CustomModule" setup_version="1.0.0">
    </module>
</config>

Step 3: Create etc/registration.php file

In this step, we will add registration.php as following guide:

app/code/W3codezone/CustomModule/registration.php

Contents would be:

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

Step 4: Enable the module

Finish the step 3, we have already created the CustomModule module. And we will enable this module in this step

After create the module if you run the command as:

php bin/magento module:status

You should see the module is disable now:

List of disabled modules: W3codezone_CustomModule

Let’s run the command as:

php bin/magento module:enable W3codezone_CustomModule

Or other way, you can access the file:

app/etc/config.php

You will see a long list of modules there, just add your module as well

...
  'W3codezone_CustomModule' => 1, 
  ...

Your module should be available now.

After this step, when you open your website in browser you will get an error saying

Please upgrade your database: Run bin/magento setup:upgrade from the Magento root directory.

Let run the command:

php bin/magento setup:upgrade
  php bin/magento setup:static-content:deploy

That’s all about the creation of custom module. Your Custom module is installed now successfully.

For Detailed video you can visit on this link: Click here

Thanks 🙂

Add a Comment

Your email address will not be published. Required fields are marked *