How to create a Basic structure of Magento 2 extension?

In this tutorial, I have created simple instruction to create a basic module for Magento 2. Once your basic structure is ready then you can add functionality according to your website or client requirements.

Follow the below steps to create simple module:

Step 1: Go to the Magento 2 root directory.

Step 2: Go to path app -> code.

Step 3: Create folder and subfolder with vendor name and module name respectively as given below.

Step 4: Create registration.php file under Bay20/Custom and put the below code.

<?php 
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bay20_Custom',
DIR
);

Step 5: Create module.xml file under Bay20/Custom/etc and put below code.

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

Step 6: Upgrade your database and regenerate files by using below commands:

 php bin/magento setup:upgrade 
php bin/magento setup:static-content:deploy

Step 7: Now you can see your installed module in app/etc/config.php as shown in the image below.

Step 8: Create a file routes.xml under Bay20/Custom/etc/frontend and put below code.

<?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=”front” id=”front”>
<module name=”Bay20_Custom”/>
</route>
</router>
</config>

Step 9: Create a Controller file Index.php under Bay20/Custom/Controller/Custom and put the below code.

<?php
namespace Bay20\Custom\Controller\Custom;
class Index extends \Magento\Framework\App\Action\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); } public function execute() 
    { 
       return $this->_pageFactory->create(); 
     }
}

Step 10: Create a Block file Index.php under Bay20/Custom/Block and put the below code.

<?php
namespace Bay20\Custom\Block;
class Index extends \Magento\Framework\View\Element\Template
{
/your code here/
}

Step 11: Create a templates file custom-test.phtml under Bay20/Custom/view/frontend/templates and put below code.

<h2>This is a basic structure of magento 2 module<h2>

Step 12: Create a layout file front_custom_index.xml and put the below code.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="content">
<block class="Bay20\Custom\Block\Index" name="custom-test" template="Bay20_Custom::custom-test.phtml" />
</referenceContainer>
</page>

Step 13: Run php bin/magento cache:clean

Step 14: Enter the following url and you will see result as image below.

http://<yourhost.com>/front/custom/index