How to create module in Magento 2 - Sample module step by step

We will learn the topic how to create Magento 2 module, In Magento 2 you can start creating your module in app/code folder.

To create the module you have to go thought the steps describe below.

#1 : You have to switch your magento mode to “developer”, you have to run following command in your terminal or console.

php bin/magento deploy:mode:set developer

#2 : Create folder in app/code with your module name.

app/code/Namespace/Modulename

#3 : Now you need to create required file module.xml inside the etc of Model Directory.

app/code/Namespace/Modulename/etc/module.xml

content of the file

#4 : Now we need to register your module so we need to create registration.php in the Module Directory.

content of the file

#5 : Best approach to enable module with following command in terminal or console.

php bin/magento module:enable Namespace_Modulename

and alternate way to enable the module is go to following file

app/etc/config.php

add the following like info the config.php file

‘Namespace_Modulename’ => 1

#6 : Now you need to run the final command to finish the sample module of Magento 2.

php bin/magento setup:upgrade

after run this command you not need to deploy the static content because you are in developer mode.

Now your module is ready to create BLOCK, CONTROLLER, MODULE, PLUGIN, OBSERVER, HELPER etc. inside it.