Magento 2 Create Simple Module

Magento 2 Create Simple Module

In this post we will show you Magento 2 Create Simple Module, hear for Magento 2 Create Simple Module we will give you demo and example for implement.

Magento 2 Create Simple Module step by step
Magento 2 Create Simple Module step by step

how to Create Magento 2 Simple Module

Hear is Steps of Magento 2 Create Simple Module.

Step 1# Create the directory of the module,

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

app/code/Onlinecode/HelloWorld

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

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

app/code/Onlinecode/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="Onlinecode_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/Onlinecode/HelloWorld/registration.php and put this content in the registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Onlinecode_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:
Onlinecode_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 Onlinecode_HelloWorld

The module has enabled successfully if you saw this result:

The following modules has been enabled:
– Onlinecode_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/Onlinecode/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="Onlinecode" frontName="helloworld">
			<module name="Onlinecode_HelloWorld" />
		</route>
	</router>
</config>

After define the route, the URL path to our module will be:

Step 7# Create Controller and Action

In this step, we will create controller and action to display Hello onlinecode. Now we will choose the url for this action. Let assume that the url will be:

So the file we need to create is:

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

And we will put this content:

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

class Display extends \Magento\Framework\App\Action\Action
{
	public function __construct(\Magento\Framework\App\Action\Context $context)
	{
		// return context
		return parent::__construct($context);
	}

	public function execute()
	{
		echo 'Hello onlinecode';
		exit;
	}
}

If you have followed all above steps, you will see Hello onlinecode when open the url

Hope this code and post will helped you for implement Magento 2 Create Simple Module. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org

Leave a Comment

Your email address will not be published. Required fields are marked *

4  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US