Step 1. Disable Magento cache (recommended)

Disabling Magento cache during development will save you some time because you won’t need to manually flush the cache every time you make changes to your code.

The easiest way to disable cache is to go to Admin ? System ? Cache Management ? select all cache types and disable them.

Step 2. Put Magento into a developer mode (recommended)

You should put Magento into a developer mode to ensure that you see all the errors Magento is throwing at you.

In order to do this, open your terminal and go to the Magento 2 root. From there you should run the following command:

php bin/magento deploy:mode:set developer

Step 3. Create the folder of Hello World module

Name of the module is defined as “VendorName_ModuleName”. First part is name of the vendor and last part is name of the module: For example: Magento_HelloWorld.

Focus on following guide to create the folders:

app/code/NS/Firstmodule

Step 4. Create etc/module.xml file

Then, it is necessary to create etc folder and add the module.xml file

app/code/NS/Firstmodule/etc/module.xml

Contents would be:

<?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="NS_Firstmodule" setup_version="0.0.1"/>

</config>

Step 5. Create etc/registration.php file

In this step, we will add registration.php as following guide:

app/code/NS/Firstmodule/registration.php

Contents would be:

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

Step 6. Enable the module

Finish the step 5, we have already created Firstmodule the module. And we will enable this module in this step

After create the module run the next commands:

php bin/magento setup:upgrade

If all is good, you will see you module in list, for example:

for example

php bin/magento setup:static-content:deploy

Step 7. Creating a controller

  •  First we need to define the router.

To do this, create a routes.xml file in the

app/code/NS/Firstmodule/etc/frontend/

folder with the following code:

<?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="firstmodule" frontName="firstmodule">
           <module name="NS_Firstmodule" />
       </route>
   </router>
</config>

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

http://<yourhost.com>/firstmodule/*

  • Create controller

The folder and file you need to create is:

app/code/NS/Firstmodule/Controller/Index/HelloWorld.php

Contents would be:

<?php
namespace NS\Firstmodule\Controller\Index;
class HelloWorld extends \Magento\Framework\App\Action\Action{
   public function execute()
   {
       echo "Hello! It’s my first module of Magento 2!";
       exit;
   }

After completed, please run sudo bin/magento cache:flush
to check result.

Your URL now should be as:

http://<yourhost.com>/firstmodule/index/helloWorld

After finish all steps, the output Hello! It’s my first module of Magento 2! should be displayed in your browser when you open the URL.

hello

5.00 / 5.0
Article rating (7 Reviews)
Do you find this article useful? Please, let us know your opinion and rate the post!
Not badGoodVery GoodGreatAwesome