Steps to create Basic Module in Magento 2

We’ll going to build basic simple module in Magento 2 which will output “Hello World” in browser. Let’s start

In Magento 2 all modules reside in the folder app/code, previously in Magento 1  there was the concept of local, community and core codePool but that has been removed now. Magento 2 is has new structure, truly fast, more structured,use jQuery as a default Javascript library instead of prototype in Magento 1.

Before creating custom module i recommend you to setup developer environment for Magento 2

Create Hello World module for Magento 2

So our first step is to create the module directory and necessary files required to register a Magento 2 module.

Step 1. Create a directory structure for the module.

In this module, we will use RK for Vendor and Helloworld for Module Name. So we need to make following directory structure :

  • app/code/RK
  • app/code/RK/Helloworld

Note: If you don’t have the code folder in your app directory, create it manually.

Step 2. Declaring module by using configuration file etc/module.xml

Magento 2 looks for configuration information for each module in those module’s etc directory. We need to create module.xml at following directory :

app/code/RK/Helloworld/etc/module.xml

add following content in it:

<?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="RK_Helloworld" setup_version="1.0.0" >
</config>

Step 3. Register module by using registration.php

Each Magento 2 module must be registered in the Magento system through the magento ComponentRegistrar class. We need to create registration.php at following path:

app/code/RK/Helloworld/registration.php

Add following content to above file:

<?php

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

Step 4. Enable the module through Terminal

By performing above steps, you have created empty module. Now we will going to enable it in Magento website.

Open terminal and go to your Magento 2 root and run following command to enable this module.

php bin/magento module:enable RK_Helloworld

The module is enable successfully if you see following result:

The following modules has been enabled:
- RK_Helloworld

It’s the first time you have enable this module so Magento 2 require to check and upgrade module database schema. We need to run this following command:

php bin/magento setup:upgrade

If you want to make yourself sure that the module is installed, you can go to Admin → Stores → Configuration → Advanced → Advanced and check that the module is present in the list or you can also open app/etc/config.php and check the array for the ‘RK_Helloworld’ key, whose value should be set to 1.

Step 5. Define a Router for the module

In Magento 2 URL’s are structured this way:
http://magento2.loc/<frontName>/<controller_name>/<action_name>/<param_1>/<param_2>/

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

app/code/RK/Helloworld/etc/frontend/routes.xml

Add following content in above 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="helloworld" frontName="helloworld">
            <module name="RK_Helloworld" />
        </route>
    </router>
</config>

The frontName attribute is going to be the first part of our URL. So in our module, the final URL will look like following:

http://magento2.loc/helloworld/*

Step 6. Create controller and action for our module.

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://magento.local/helloworld/index/index

We need to create following file :

app/code/RK/Helloworld/Controller/Index/Index.php

Add following content in to it:

<?php
namespace RK\Helloworld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
  public function execute()
  {
    echo 'Hello World';
    exit;
  }
}

Step 7. Clear Cache.

The easiest way to clear cache is to go to Admin → System → Cache Management → select all cache types and refresh them.

If you have performed all above steps, you can see “Hello World” when open the  URL : http://magento2.loc/helloworld/index/index in your browser and you see following output:

Magento 2 simple basic hello world module

If you have any query in creating basic module in magento 2, leave it in the comment segment, I will response as soon as possible:

Advertisements

One thought on “Steps to create Basic Module in Magento 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s