A module is a group of directories that contains controllers, blocks, models, helpers that are developed by third-party programmers. Specifically, a Magento module is composed of these software components: themes, libraries, and language packages. These modules generally extend core modules and offer their own functionality that can often be used anywhere in Magento.

In this topic, we will create a Magento 2, It’s following by 6 steps:

Step 1: Creating the module files and folders
Step 2: Declare module by using configuration file module.xml
Step 3: Register the module by registration.php
Step 4: Install Setup, Enable the module
Step 5: Create a route for the module
Step 6: Create a controller and action

Step 1: Creating the module files and folders

Create the following folders:
app/code/First
app/code/First/Helloworld

The First folder is the module’s namespace, and Helloworld is the module’s name.

Note: If you don’t have the code folder in your app directory, create it manually.

Step 2: Declare module by using configuration file module.xml
Then, it is necessary to create an etc folder and add the module.xml file

app/code/First/Helloworld/etc/module.xml

 

 

 

Step 3: Register the module by registration.php

To register the module, create a registration.php file in the app/code/First/Helloworld folder with the following code:

 

 

 

 

Step 4: Install Setup, Enable the module
After creating all files of the above steps. We can install the module through the command line. Please open your terminal go to the project folder and use these commands

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

After deploy completed, you can check your module from backend at System Configuration -> Advanced -> Disable Modules Output.

Step 5: Create a route for the module
To add route, it is necessary to create routes.xml file

app/code/First/Helloworld/etc/frontend/routes.xml

since this is a frontend route, we added it in frontend/ folder else we need to add it to adminhtml/ folder

 

 

 

 

After defining the first part of the route, the URL will be displayed as:

http://<yoursitename.com>/helloworld/*

Step 6: Create a controller and action
Now we create the Index.php controller file in the app/code/First/Helloworld/Controller/Index folder with the following code:

 

 

 

 

 

 

 

If you are familiar with Magento 1 controllers, then you know they can have multiple actions. In Magento 2 controllers have only one method (execute) that will be called by front controller.

Creating a block

We’ll create a simple block class with the getHelloWorldTxt() method which returns the “Hello world” string.

  1. Create a Helloworld.php file in the app/code/First/Helloworld/Block folder with the following code:

 

 

 

 

Creating a layout and template files

In Magento 2 structure, templates and layout files are placed in the view folder inside your module. Inside the View folder, we can have three subfolders: base, frontend and adminhtml.

The base folder is used for both, frontend and admin files. The frontend folder is used for frontend and the adminhtml folder is used for admin.

1. First we will create a helloworld_index_index.xml file in the app/code/Inchoo/Helloworld/view/frontend/layout folder with the following code:

 

 

Create a Helloworld.phtml file in the app/code/Inchoo/Helloworld/view/frontend/templates folder with the following code:

 

 

And that’s it. Open the /helloworld/index/index URL in your browser.