C O D I T R O N

Technologies

How To Create A Basic Module In Magento2

By, Kiran Nanaware
7th September 2022

So if you are creating the very first module in Magento, you have come to the right place because this blog will guide you on how to create the basic module with the proper file structure. To get that right please follow the module creation procedure with the commands given below.

To start with creating module go to app/code folder. Here is where we begin with module creation.

Let us create a basic “Welcome” module.

Module Folder Creation:

Make sure you are in the app/code folder. In that you have to create a vendor folder and in vendor folder, you have to create your module folder.

In the code folder create two new folders like app/code/Vendor_Name/Module_Name. When you are using this module in the program you have to mention the module as “VendorName_ModuleName”

For example “Coditron_Welcome”. Here Coditron is our vendor_name and Welcome is our module_name.

So we have created the module and its path is app/code/Coditron/Welcome. Let’s have a look at the module file structure so that you can get a clear idea.

Step 1: Module File Structure.

Create folders and files as shown below in the file structure. The file structure of your module should be as given below:

Step2: Create a registration file:

After this, you have to create a registration.php file. This file is very important because in this file your module is registered. 

Step 3: Create Module XML file:

Now create module.xml file in app/code/Coditron/Welcome/etc folder.

Step 4: Run the below commands:

1) To check module status:

  • php bin/magento module:status

2) You will get a module list, in that disable modules list our module name is present so, let us enable our module by the below command:

  • php bin/magento module:enable Coditron_Welcome

Your module is enabled. And you can check whether your module is enabled or not by viewing if the value of your module name is set to “1”.

Open app/etc/config.php file and check the value.

‘Coditron_Welcome’ => 1,

Now run the below commands to upgrade and deploy your Magento.

  • php bin/magento setup:upgrade
  • php bin/magento setup:static-content:deploy -f
Step 5: Create routes file:

After these commands create the routes.xml file in the app/code/Coditron/Welcome/etc/frontend/ folder.

Step 6: Create a Controller:

Create a file Index.php in the app/code/Coditron/Welcome/Controller/Index/ folder.

Step 7: Run the below command to flush the cache:
  • php bin/magento cache:flush

Now Your Module is Ready.

So that is the first module in Magento2. I hope you enjoyed this blog. Isn’t it exciting? If you want to learn about different modules you can follow my other blogs.

Thank You.

Coditron Technologies

Contact Us

Address

Corporate Office : Greens Center, Thergaon Pune 411033

Operation Office: USA: PO Box # 582973 ElkGrove CA 95758