Step 6: Create controller and action

In the last step, we will create url for displaying in your browser: “Hello Magento 2. We will change the world”.

Create an action file:

app/code/Magestore/HelloMagento/Controller/Index/Index.php

Add the content:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?php

namespaceMagestore\HelloMagento\Controller\Index;

classIndexextends\Magento\Framework\App\Action\Action

{

publicfunction__construct(

\Magento\Framework\App\Action\Context$context)

{

returnparent::__construct($context);

}

publicfunctionexecute()

{

echo'Hello Magento 2! We will change the world!';

exit;

}

}

Result:

Open your browser, enter this link: http://<magento_url>/hellomagento/index/indexand look at the result:

These are all steps to create a new module in Magento 2. Hope all you guys can learn Mmagento 2 more easily with our series lesson. In next topic, we will show you how to create a view, block, template in Magento 2.

The 6 steps I mentioned above is the shortest process for you to Create a Magento 2 module. With this guide, you can manage the Module in Magento 2 easily. Every store has a Module in Magento 2 with many attributes.