If you are creating the first module in Magento this blog is helpful for you because for that you have to know the file structure of the module and guide on how to create it. To get that right please follow the module creation procedure with the commands given below.
Let us create a first “Welcome” module:
Module folder creation:
Firstly, you have to know whenever you are creating a module you have to go app/code/ folder, now in the code folder create two new folders like 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, Coditron_sales etc.
Now go to your Magento2 folder and create the module app/code/Coditron/Welcome
Step1: Module File Structure:
The file structure of the module is 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 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”.
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 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? Let’s learn more about different modules in my next blog.
Target keywords: Simple module, Magento module, Module, the first module, magento2