How to create a simple Hello World module in Magento 2?

Create a Simple Hello World Module using Magento 2 is the Entry point for Learning a Magento 2.

Simple Module to display, Hello World in the browser is used for Beginner of Magento Developer who wants to explore Magento 2 in depth.

You must need to create two entry point file registration.php and module.xml file to define the Custom Module.

  • registration.php — Used to Register New Module In Magento 2 System.
  • module.xml — XML file defines our module name, Also contains <sequence> tag for Dependency of Other modules in our module.

The module is simply residing in the app/code folder of Magento instance.

Just Create Folder under the app/code,
For example, We kept Packagename as Rbj and Modulename as Training.
You can keep any name for Packagename/Modulename. In our demo, we have kept Packagename_Modulename as Rbj_Training.

Now start with basic module creation, Registration.php file is to register our module.  Create registration.php file,

Full Path,  app/code/Rbj/Training/registration.php,

Create module.xml file,
app/code/Rbj/Training/etc/module.xml,

In above file keep name attribute equal to your {Packagename_Modulename}

Now create routes.xml file,
app/code/Rbj/Training/etc/frontend/routes.xml,

The frontName attribute is the first part of our custom URL. Default URL structure for Magento 2 like,  <frontName>/<areaControllerFolderName>/<controllerClass>

Where areaControllerFolderName is your area’s controller Folder Name.
An area might be front or admin area. In our case, <areaControllerFolderName> equals Index and   <controllerClass> equals Training for our Module.

Create <controllerClass>,
app/code/Rbj/Training/Controller/Index/Training.php

In Magento 2 Controller file has execute() method, Its execute when our controller is called.  Now our final URL will look like this, training/index/training

Creating a layout and template files for our module. First, create a training_index_training.xml file in the app/code/Rbj/Training/view/frontend/layout folder with the following code:

In the above content, Block class is Rbj\Training\Block\Index\Training, We need to define Block for keeping our templates dynamic function. We will create a block further in this tutorial.

Create template file training.phtml for our module representation in frontend,
app/code/Rbj/Training/view/frontend/templates/index/training.phtml,

Create Training.php block class for a dynamic function which is required to call in our template file.
The file path, app/code/Rbj/Training/Block/Index/Training.php

Now When you hit the URL, {{Site_Base_Url}}/training/index/training,
You will redirect to the 404 pages because our module is not installed yet.

For installing our module in Magento 2,  We need to run the command using SSH,  Login with SSH and go to your project root path where Magento is installed,
Run below command,

Now You run the URL in the frontend,
{{Site_Base_Url}}/training/index/training
You have a display message which you have typed in the template file.

Result is,

create a simple Hello World module in Magento 2

Using the above steps, You can create your own Custom Module in Magento 2 with easy steps.