Create Simple Module in Magento 2

It’s time to learn what’s new about Magento 2.

In this article, I will share my knowledge on Custom module creation in Magento 2.

Step 1 :

To initiate the module you have to create a file called, module.xml under the directory: app/code/<Namespace>/<Modulename>/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="<Namespace_Modulename>" setup_version="1.0.0">
    </module>
</config>
    

In this file, Namespace, Custom Module name and the module version has to be mentioned.

Read: Creating Custom Attribute Set in Magento 2 via UpgradeData.php

Step 2 :

After that initiates, the module needs to be registered. In case you’re using a system built on the official Magento 2.0 release, then there’s one more thing that needs to be taken care of. In addition to the files mentioned above, every module also needs a registration.php file which looks like this:

In , app/code/<Namespace>/<Modulename>/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE,
'<Namespace_Modulename>',
__DIR__ );

Step 3 :

To activate the Module, edit the config.php under app/etc

In the file add module with the array module.

Ex : ‘Namespace_Module’ => 1,

array (
‘Namespace_Module’ => 1, // Add like this
'Magento_Store' => 1,
'Magento_AdvancedPricingImportExport' => 1,
'Magento_Directory' => 1,
'Magento_Theme' => 1,
'Magento_Backend' => 1, ………

Step 4 :

Next, create a Controller action for the module.

To do that, create a php file in the directory: app/code/<Namespace>/<Modulename>/Controller/<Controllername>/<Actionname>.php

Ex : app/code/<Namespace>/<Modulename>/Controller/Index/Index.php

namespace <Namespace>\<Modulename>\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
	public function execute()
	{
		$this->_view->loadLayout();
		$this->_view->getLayout()->initMessages();
		$this->_view->renderLayout();
	}
}

Where
→ Folder Index pointing the Controller name and
→ Index.php pointing Action name.

Step 5 :

Let’s Create a simple Block file for our Module in
app/code/<Namespace>/<Modulename>/Block/<Blockname>.php

Under Add code as below

<?php
namespace <Namespace>\<Modulename>\Block;
class <Blockname> extends \Magento\Framework\View\Element\Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
}

Step 6 :

After Block, create a routes.xml file to configure
Add routes.xml under,
app/code/<Namespace>/<Modulename>/etc/frontend/routes.xml

And the code inside the file (routes.xml)

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="<module>" frontName="<module>">
            <module name="<Namespace_Module>" />
        </route>
    </router>
</config>

Step 7 :

Now a Template file needs to be created, in the directory:
app/code/<Namespace>/<Modulename>/view/frontend/templates/<filename>.phtml

Add the following content in that phtml file,

<h1> Thanks for reading Apptha Blog</h2>
<h1> Success ...!!!</h1>

Step 8 :

At last to complete the creation of our Module, we need to create a layout file under,
app/code/<Namespace>/<Modulename>/view/frontend/layout/
<module_controllername_actionname>.xml

Add the following content in that file,

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Welcome ...</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block name="<blockname>" template="<filename>.phtml"></block>
        </referenceContainer>
    </body>
</page>

That’s it.. All Done..!
Run your module, in browser as,

http://localhost/magento2/welcome/index/index

To verify your module, go to terminal and run the following commands.
$ cd [your/magento/project/folder/path]
$ bin/magento setup:upgrade

This will list all the modules list in your Magento 2.

Realated Posts

2 thoughts on “Create Simple Module in Magento 2

  1. Vijay

    Hi,

    It’s little strange you guys don’t have a contact number, email me your contact number, would like to talk you guys

    Thanks

    • Ramanathan

      Dear Vijay,

      Thanks for showing your interest, Kindly share your requirement specification and here I am sharing the presales specialist number & email address pradeep.t@contus.in Pradeep: +91 9790922558

Leave a Reply

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

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">