PASSWORD RESET

Your destination for complete Tech news

How to create a simple Hello World module in Magento 2?

213 0
2 min read

To create a simple “Hello World” module in Magento 2, you will need to perform the following steps:

1. Create a new directory for your module under app/code. The directory should be named after your module, using the format Vendor_Module. For example, if your module is called “HelloWorld”, the directory should be named HelloWorld.

2. Create a registration.php file in the root of your module directory. This file should contain the following code:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Replace Vendor_Module with the actual name of your module (e.g. HelloWorld). This file tells Magento that your module exists and where to find it.

3. Create a module.xml file in the etc directory of your module. This file should contain the following 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="Vendor_Module" setup_version="1.0.0">
    </module>
</config>

Again, replace Vendor_Module with the actual name of your module. This file tells Magento the version of your module and provides other information about it.

4. Create a frontend directory in the etc directory of your module. Inside the frontend directory, create a routes.xml file. This file should contain the following 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 id="helloworld" frontName="helloworld">
            <module name="Vendor_Module" />
        </route>
    </router>
</config>

This file defines a new router with a front name of helloworld, which will be used to access your module.

5. Create a Controller directory in the Controller directory of your module. Inside the Controller directory, create a Index directory. Inside the Index directory, create a Index.php file. This file should contain the following code:

<?php

namespace Vendor\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        echo "Hello World!";
    }
}

This code defines a simple controller that will output “Hello World!” when accessed.

6. Run the following command to enable your module:

php bin/magento module:enable Vendor_Module

7. Run the following command to upgrade the database schema and install the module:

php bin/magento setup:upgrade

To test your module, you can visit the following URL in your browser:

http://yourstore.com/helloworld/index/index

You should see the message “Hello World!” displayed on the page.

That’s it! You have now created a simple “Hello World” module in Magento 2. You can now start building out your module by adding more functionality and customizing the behavior of your controller.

Leave A Reply

Your email address will not be published.

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