Magento 2 Module Development – Simple Hello World Module

Magento 2 version are mostly use for building Ecoomerce website. That why I writing a blog to introduce how to create a simple Hello World Module in magento2.

We know, In Magento2 the module is a directory that contain blocks, controllers, models, helper, etc – that are related to a specific business feature. The etc folder contains admin ACL, admin menu, configuration . In Magento 2, modules will be build in app/code directory of a Magento2 installation, with this following format: app/code/<VendorName>/<ModuleName>. Now we will follow the simple steps to create a simple module which will work on Magento2 version and display Plant Market.

Basic Features of Magento2:-

  • Create Controller
  • Create Model
  • Create View
  • Create etc
  • Create Admin Menu
  • Create composer.json
  • Create registration.php
  • Configuration
  • Admin Grid

Create Plant Market module for Magento 2.

To create Plant Market module, we need to complete the following steps:-

Step 1:=> Create the folder name as Vendor name .
Step 2:=> Then the folder name as Module name inside Vendor name(which we create) folder .
Step 3:=> Create module.xml file .
Step 4:=> Create registration.php file inside the Module Folder as app/code/Plant/Market/registration.php.
Step 5:=> Create composer.json file inside the Module Folder as app/code/Plant/Market/composer.json.php.
Step 6:=> Enable the module.

Step 1:- Create the folder name Plant Market Module-

So we have to create the folder as app/app/code/VendorName:-

Magento2 Format:

app/code/VendorName

Example:-

Here we define Module as Plant_Market according to VendorName_ModuleName. First part is Plant is Vendor Name.

app/code/Plant

Step 2:-Then the folder name as Module name inside Vendor name(which we create) folder.

So we have to create the folder s Module Name in app/code/VendorName/ModuleName:-

Magento2 Format:-

app/code/VendorName/ModuleName

Example:-

we have to create the folder Market= ModuleName in app/code/Plant/Market:-

app/code/Plant/Market

Step 3:-Create module.xml file .

We have to create the module.xml in this location as app/code/Plant/Market/etc/module.xml.

app/code/Plant/Market/etc/module.xml

Paste the following code in 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="Plant_Market" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Backend"/>
         </sequence>
    </module>
</config>

Step 4:=> Create registration.php file inside the Module Folder.

so we have to create the registration.php file in this following location as app/code/Plant/Market/registration.php.

Paste the below code in registration.php:-

The Magento2 registration.php Format:-

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'VendorName_ModuleName',
    __DIR__
);

Example:-

app/code/Plant/Market/registration.php
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Plant_Market',
    __DIR__
);

Step 5:=>Create composer.json .

We have to create file inside the Module Folder as-

app/code/VendorName/ModuleName/composer.json.php

Paste the following code in composer.json according to Magento 2 format:-

{
    "name": "vendorname/modulename",
    "description": "VendorName ModuleName",
    "require": {
      "php": "~5.6.0|7.0.2|~7.0.6",
      "magento/framework": "0.74.0-beta4",
      "magento/magento-composer-installer": "*"
    },
    "type": "magento2-module",
    "version": "0.74.0-beta4",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "extra": {
        "map": [
            [
                "*",
                "VendorName/ModuleName"
            ]
        ]
    }
}

Example:-As Plant Market Module

Location path of Module-

app/code/Plant/Market/composer.json.php

Paste the code:-

{
    "name": "plant/market",
    "description": "Plant Market",
    "require": {
      "php": "~5.6.0|7.0.2|~7.0.6",
      "magento/framework": "0.74.0-beta4",
      "magento/magento-composer-installer": "*"
    },
    "type": "magento2-module",
    "version": "0.74.0-beta4",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "extra": {
        "map": [
            [
                "*",
                "Plant/Market"
            ]
        ]
    }
}

Step 6:=> Enable the module.

After this we have to run the following command to enable VendorName_ModuleName .

php bin/magento module:enable VendorName_ModuleName

Else

php -d memory_limit=10G  bin/magento module:enable [VendorName]_[ModuleName]

Example:- To enable my Plant_Market we have to run this command.

php bin/magento module:enable Plant_Market

OR

php -d memory_limit=10G  bin/magento module:enable Plant_Market

when you open your website in browser we will get an error in browser.So we have to upgrade database.so we will this following command from root directory:-

php bin/magento setup:upgrade

or

php -d memory_limit=10G  bin/magento setup:upgrade

Step 7:-Create Route

After deploy completed, we can also see our module from backend
Now, we will create a controller to test module from frontend.

Before create a controller, we have to create a route for VendorName_ModuleName/Plant_Market module.

Route in magento2 are divided into 3 parts:-

  1. Route frontname
  2. Controller
  3. Action name

As following Magento2 Format:-

http://<hostname>/<frontname>/<controller>/<action name>

To add route name and route id, it is necessary to create routes.xml file in following location:-

app/code/<VendorName>/<ModuleName>/etc/frontend/routes.xml

Example:-

Routes.xml path in our Plant Market Module

app/code/Plant/Market/etc/frontend/routes.xml

since this is a frontend route, we added it in frontend/ folder else we need to add it to adminhtml/ folder

Then Paste the following Code according to this Magento2 format:-

<?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 frontName="frontname" id="frontid"
            <module name="VendorName_ModuleName"/>
        </route>
    </router>
</config>

Example:-

Here frontname= plantname and frontid=plantid in Plant_Market Module.

<?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 frontName="plantname" id="plantid">
            <module name="Plant_Market"/>
        </route>
    </router>
</config>

After defining the first part of the route, the URL will be displayed as:-
http://<your host name>/<frontname>/*

In localhost format will be:-
E.g: http://localhost/frontname/*

Example:-

According to our Plant Market Module the format will be:-

http://sumitbera.com/plantname/*

E.g: http://localhost/plantname/*

Step 08:- Create Action file

Then, we will continue to create the controller and action
The folder and file you need to create is:
app/code//<ModuleName/Controller/Index/Test.php

As example:

app/code/Plant/Market/Controller/Index/Test.php

Then paste the following code:-

<?php


namespace Plant\Market\Controller\Index;


use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ResponseInterface;

class Test extends Action
{
    protected $_pageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }
    /**
     * @inheritDoc
     */
    public function execute()
    {
        echo "Plant Market module is working successfully";
        exit;
    }
}

After completed, please run php bin/magento cache:clean or php -d memory_limit=10G bin/magento cache:flush to check result.

Our URL now should be as:

Magento2 Format:-
http://<yourhost name>/controller/actionname

Example:- As our Plant Market Module.the browser url will be:-

http://<yourhost.com>/plantname/index/test

After finish all these steps, the output “Plant Market module is working successfully” should be displayed in out browser when you open the URL.
We hope ourblog is very useful and effective for you. Any query, feel free to leave a comment below. Your comment will inspire me to write a blog.
Thank You All.

Total Page Visits: 12577 - Today Page Visits: 1