Magento Community vs Enterprise Edition: What is the Difference?
January 12, 2022

The module is fundamentally an element in the structure in Magento 2. A brand new system is built around modules part of Magento 2. The first step to building an individual eCommerce store is to build modules.

In this tutorial, we’ll help you follow a step-by-step guide to create a custom-built module. The steps you need to follow do the module:

Here’s the full procedure:

Step 1 Create the Magento/HelloWorld module folder within your app/code directory.

Step 2: Create etc/module.xml file.

Step 3. Create the registration.php file in the directory HelloWorld/Magento.

Step 4: Turn on the module.

Now let’s go with these steps in detail:

Step 1: Module name is defined as VendorName_ModuleName.

For instance:

  • The module’s name is Magento_HelloWorld.
  • Directory will be: app/code/Magento/HelloWorld

Step 2: Create file module.xml in directory app/code/Magento/HelloWorld/etc

Step 3. Create the registration.php file within the directory HelloWorld/Magento.

<?php

/**

* Copyright © Magento, Inc. All rights reserved.

* See COPYING.txt for license details.

*/

\Magento\Framework\Component\ComponentRegistrar::register(

    \Magento\Framework\Component\ComponentRegistrar::MODULE,

    ‘Magento_HelloWorld’,

    __DIR__

);

?>

Step 4: Turn on the module by following:

After creating the module, you must run the command from the terminal. For example: PHP bin/Magento module:status

To turn on the module, execute the following command: PHP bin/Magento module: enable Magento_HelloWorld.

The module will be done in the following manner:

  • If you go to the URL of the website, you will see an error message.
  • To fix the issue, you must run the upgrade command within the Magento root directory, such as in the following: PHP bin/Magento setup: upgrade
  • We now need to design an option for the HelloWorld module:
  • Magento is split into 3 components: Route name controller, Action and front name. e.g., http://localhost/frontname/controller/action
  • To add a new route, it is essential to make a routes.xml file: app/code/Magento/HelloWorld/etc/frontend/routes.xml
  • As this is a frontend routing, we put it in the frontend folder. If you plan to build routes for the backend, you have to include them in the adminhtml/folder.

<?xml version=”1.0″?>

<!–

/**

* Copyright © Magento, Inc. All rights reserved.

* See COPYING.txt for license details.

*/

–>

<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=”Magento_HelloWorld” />

        </route>

    </router>

</config>

  • Then , create a controller as well as an action.
  • The folder and file that you need to create is: app/code/Magento/HelloWorld/Controller/Index/Test.php

<?php

/**

* Copyright © Magento, Inc. All rights reserved.

* See COPYING.txt for license details.

*/

namespace Magento\HelloWorld\Controller\Index;

use Magento\Framework\App\Action;

class Test extends 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()

    {

        echo “Hello World Test Controller”;

        exit;

    }

}

?>

  • Then run this command: PHP bin/Magento cache: clean
  • Your URL now should be http://localhost/helloworld/index/test
  • Output “Hello World Test Controller” should be displayed.

Leave a Reply

Your email address will not be published. Required fields are marked *

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