Advertisements

Today we are going to write Magento 2 basic hello world Module.

For me as a developer trying something I always try to print out hello world First. I have no idea why I do that but it’s like a starting point for me. So let get started with Printing out hello world in Magento 2 on the website with our own extension with custom URL.

For creating our extension we need:

  • We need to register our module

  • Creating a controller

  • Deploy with command

Path : app/code/Nilesh/HelloWorld/registration.php
<?php
     
    use Magento\Framework\Component\ComponentRegistrar;
    
    ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Nilesh_HelloWorld', __DIR__);

?>

Path : app/code/Nilesh/HelloWorld/etc/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="Nilesh_HelloWorld" setup_version="1.0.0" />
</config>

Path : app/code/Nilesh/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="customer" id="customer">
            <module name="Nilesh_HelloWorld"/>
        </route>
    </router>
</config>

Path : app/code/Nilesh/HelloWorld/Controller/Index/Index.php
<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Nilesh\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\Json\Helper\Data $jsonHelper
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        echo "Hello World"; exit();
    }
}

Command : php bin/magento setup:upgrade
Command: php bin/magento setup:static-content:deploy -f
Command : php bin/magento c:f

We use “php bin/magento setup:upgrade" command to make our module register or enable. Actually, for enabling our module in another word it’s also an extension. we have to use “bin/magento module:enable Vendor_Module" for me, it will look like this “bin/magento module:enable Nilesh_HelloWorld".

But use “php bin/magento setup:upgrade" it will do all the other stuff for you. Then you have to use deploy command “php bin/magento setup:static-content:deploy -f" to generate a static file which stays in pub/static directory and also if you are curious of “-f" use in deploy command which is optional flag than take it as we are forcefully run deploy to generate static files and its work perfectly

Also plus point, Please always keep the habit to use cache flush.

You all are the semicolon to my statements; Please support me; Thanks