Create a Simple Magento 2 Module

Create a HelloWorld Module in magento 2

Steps of creating a simple magento 2 module.

Step 1: Create the directory of the module,

In this module, we will use Bluethink for Vendor name and HelloWorld for ModuleName. So we need to make this folder:

app/code/Bluethink/HelloWorld

Step 2: Declare module by using configuration file module.xml

We need to create folder etc and add module.xml:

app/code/Bluethink/HelloWorld/etc/module.xml

and put this content in the module.xml

<?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=”Bluethink_HelloWorld” setup_version=”1.0.0″ />
</config>

Step 3: Register module by registration.php

We need to create a file named registration.php in HelloWorld directory.

app/code/Bluethink/HelloWorld/registration.php

and put this content in the registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
‘Bluethink_HelloWorld’,
__DIR__
);
Step 4: Check status of module

For checking the status of module run the following command:

php bin/magento module:status

If you follow above step, you will see this in the result:

List of disabled modules:
Bluethink_HelloWorld

Step 5: This means the module has recognized by the system but it is still disabled. Run this command to enable it:

php bin/magento module:enable Bluethink_HelloWorld

The module has enabled successfully if you saw this result:

The following modules has been enabled:
– Bluethink_HelloWorld

This’s the first time you enable this module so Magento require to check and upgrade module database. We need to run this comment:

php bin/magento setup:upgrade

Now you can check under Stores -> Configuration -> Advanced -> Advanced that the module is present.

Step 6: Create a Routers for the module.

 

The Router is used to assign a URL to a corresponding controller and action. In this module, we need to create a route for frontend area. So we need to add this file:

app/code/Bluethink/HelloWorld/etc/frontend/routes.xml

And content for this file:

<?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=”Bluethink” frontName=”helloworld”>
<module name=”Bluethink_HelloWorld” />
</route>
</router>
</config>

After define the route, the URL path to our module will be: http://example.com/helloworld/

Step 7: Create Controller and Action

In this step, we will create controller and action to display Hello World. Now we will choose the url for this action. Let assume that the url will be: http://example.com/helloworld/index/display

So the file we need to create is:

app/code/Bluethink/HelloWorld/Controller/Index/Display.php

And we will put this content:

<?php
namespace Bluethink\HelloWorld\Controller\Index;

class Display 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;
}
}

If you have followed all above steps, you will see Hello World when open the url http://example.com/helloworld/index/display