STEPS TO CREATE SIMPLE HELLOWORLD MODULE IN MAGENTO2

This Helloworld module will create a Controller that will open a page with content from a phtml file

Step1: Create a vendor folder inside app/code (app is the core folder in magento)

Folder :-app/code/Pravams (Vendor’s name could be any)

Step 2: Create a module folder inside app/code/Pravams

Folder:- app/code/Pravams/Helloworld

Step 3: Now inside Helloworld create a registration.php file.

File:- app/code/Pravams/Helloworld/registration.php

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

Step 4: Create Block folder inside app/code/Pravams/Helloworld

File:- app/code/Pravams/Helloworld/block

Now after creating Block folder create Index.php file inside Block folder and then write the given code

<?php
namespace Pravams\Helloworld\Block;
class Index extends \Magento\Framework\View\Element\Template
{
    
}

Step 5: Inside module create a folder Controller , inside Controller create a folder Index and inside Index create Index.php file and write the given code.

File:- app/code/Pravams/Helloworld/Controller/Index/Index.php

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

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 6:- Now create etc folder inside module, then create module.xml file and write the given code

File:- app/code/Pravams/Helloworld/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemeLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Pravams_Helloworld" setup_version="1.0.0">
    </module>

</config>

Now again inside etc folder create a frontend folder and inside frontend folder create routes.xml file, inside routes.xml write the given code

File:- app/code/Pravams/Helloworld/etc/frontend/routes.xml

<?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="helloworld" id="helloworld">
            <module name="Pravams_Helloworld"/>
        </route>
    </router>

</config>

STEP 7: Inside module create a view folder, inside view create frontend folder, inside frontend create layout folder, inside layout create helloworld_index_index.xml file and then write the given code

File: app/code/Pravams/Helloworld/view/frontend/layout/helloworld_index_index

<?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">

<body>   
    <referenceContainer name="content"> 
        <block class="Pravams\Helloworld\Block\Index" name="index.index" template="Pravams_Helloworld::index/index.phtml" />
    </referenceContainer>
    
</body>
</page>

Now inside layout folder create a templates folder, inside template folder create index folder , inside index folder create index.phtml file and then write the given code

File: app/code/Pravams/Helloworld/view/frontend/templates/index/index.phtml

<h2>Welcome to Pravams.com</h2>

STEP 7: After writing all the above commands go to the Linux Terminal then RUN the following commands


sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento cache:flush
sudo chmod 777 -R var/ pub/ generated/

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.