How to create simple module in Magento2

Module is an important part of Magento2 which helps developers to customize this platform. Each module has its specific functions, therefore, learning how to create a module is the very first basic knowledge for every Magento developer. In this blog, we will introduce the way to work with a Magento module with simple

Step 1: Create your module folder

We must declare a module is VendorName_ModuleName. In our example, we have a module named MGS_MyModule

Path: app/code/MGS/MyModule

Step 2: Declare your new module

Path: app/code/MGS/MyModule/etc/module.xml

This file helps Magento to recognize your module along with the version it will be upgraded to. Create a file as the above path with the content below:

 
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MGS_MyModule" setup_version="1.0.0" />
</config>

Step 3: Register your new module

Path: app/code/MGS/MyModule/registration.php

This file helps to register your module with Magento system. Create the file to add this content:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MGS_MyModule',
    __DIR__
);

Step 4: Enable your new module

We will use commands to work in this step. Open terminal in your root Magento folder as below instructions:

When you first create your new module, you may need to view the list of module to make sure your module is recognized:

php bin/magento module:status

You will see your new module at the end of the disabled module list. To enable it, type:

php bin/magento module:enable MGS_MyModule

To make sure it runs well, type:

php bin/magento setup:upgrade

To prevent the layout from being broken, type:

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

In case you want to disable your module, type:

php bin/magento module:disable MGS_MyModule

Step 5: Create a route name for your new module

As you know, the route is the URL path which helps to connect to specific pages. In Magento, we can easily declare it using a file rule named routes.xml

Route path contains 3 parts: [route_name] / [controller_name] / [action_name]

Create a file as path: app/code/MageSolution/MyModule/etc/frontend/routes.xml with this content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
         <route id="magesolution" frontName="helloworld">
             <module name="MGS_MyModule" />
         </route>
    </router>
</config>

Note that the frontName helloworld will be your route_name.

Step 6: Create a controller name and action name for your new module

In this step we will declare the other 2 parts of URL path.

Create the action file as: app/code/MGS/MyModule/Controller/Index/Index.php

Note that, the folder name Index will be your controller_name, and the file name Index will be your action_name, based on the route path we mentioned in step 5.

Add this below content:

<?php
namespace MGS\MyModule\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
  public function __construct(
\Magento\Framework\App\Action\Context $context)
  {
    return parent::__construct($context);
  }

  public function execute()
  {
    echo 'Hello World';
    exit;
  }
}

Step 7: See the result

Open your Magento folder terminal, type:

php bin/magento cache:clean

Now open your web browser and connect to this URL:
http://[your_magento_url] /helloworld/index/index to see what you have done.

Hope this blog will help you to conquer the first steps in Magento journey. Thanks for reading!