Very easy to create a custom module for Magento 2.3.7. Only 5 steps, follow me, and I will teach you how to do step by step.
Step 1: Create the folder of Glory Eyeglasses module
Step 2: Create etc/module.xml file
Step 3: Create etc/registration.php file
Step 4: Enable the module
Step 5: Create a route
Step 6: Create a controller
To create Hello World module, you need to complete the following high-level steps:
Step 1: Create the folder of Glory Eyeglasses module
Name of the module is defined as “Glory_Eyeglasses”. First part is name of the vendor and last part is name of the module: For example: Glory_Eyeglasses, Glory_One_step_checkout. Focus on following guide to create the folders:
<pre>
app/code/Glory/Eyeglasse
</pre>
a. namespace?Here I use ‘Glory’
b. module?Here I use ‘Eyeglasses’
Step 2: Create etc/module.xml file
Then, it is necessary to create etc folder and add the module.xml file
<pre>
app/code/Glory/Eyeglasses/etc/module.xml
</pre>
Contents would be:
<pre>
<?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=”Glory_Eyeglasses” setup_version=”1.1.0″/>
</config>
</pre>
Step 3: Create etc/registration.php file
In this step, we will add registration.php as following guide:
<pre>
app/code/Glory/Eyeglasses/registration.php
</pre>
Contents would be:
<pre>
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
‘Glory_Eyeglasses’,
__DIR__
);
</pre>
Step 4: Enable the module
After create the module if you run the command as:
<pre>
php bin/magento module:status
</pre>
You should see the module is disable now:
List of disabled modules: Glory_Eyeglasses
Follow exact guide to enable the module right now, let run the command as:
<pre>
php bin/magento module:enable Glory_Eyeglasses
</pre>
Your module should be available now.
After this step, when you open your website in browser you will get an error saying
Please upgrade your database: Run “bin/magento setup:upgrade” from the Magento root directory.
Let run the command:
<pre>
php bin/magento setup:upgrade
</pre>
Sometimes we may need to run the following commands:
<pre>
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy
php bin/magento c:c
php bin/magneto c:f
</pre>
Step 5: Create a route
To add route, it is necessary to create routes.xml file
<pre>
app/code/Glory/Eyeglasses/etc/frontend/routes.xml
</pre>
since this is a frontend route, we added it in frontend/ folder else we need to add it to adminhtml/ folder
Content would be:
<pre>
<?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=”eyeglasses” frontName=”glasses-online”>
<module name=”Glory_Eyeglasses” />
</route>
</router>
</config>
</pre>
After define the first part of the route, the URL will be displayed as:
<pre>
http://<yourhost.com>/helloworld/*
https://www.gloryeyeglasses.com/glasses-online/*
</pre>
Step 6: Create a controller
After we have add the rout, next we will continue the controller and action
The folder and file you need to create is:
<pre>
app/code/Glory/Eyeglassses/Controller/Cheap/Index.php
</pre>
Contents would be:
<pre>
<?php
namespace Glory\Eyeglasses\Controller\Cheap;
class Index extends \Magento\Framework\App\Action\Action
{
public function __construct(\Magento\Framework\App\Action\Context $context)
{
return parent::__construct($context);
}
public function execute()
{
echo ‘<center>Hello World! Buy eyeglasses online at an affordable price and save!</center>’;
exit;
}
}
</pre>
After completed, please run php bin/magento cache:clean to clear cache and check result.
Your URL now should be as:
<pre>
https://www.gloryeyeglasses.com/glasses-online/cheap
</pre>