8

I'm new to magento2.

I want to create simple Hello World module in magento 2.

How to create this module?

1
  • 2
    Sounds to me like someone can't use a search engine or is too lazy to do so. Commented Jun 23, 2016 at 8:01

10 Answers 10

8

Well this is a broad question but my best advice would be to check the official Magento 2 samples.

You can find them here: https://github.com/magento/magento2-samples

This project is a collection of samples to demonstrate technologies introduced in Magento 2. You will find the most simple extension along with samples that incrementally add features to lead you through a exploration and education of the Magento 2 platform.

On top of that you can find many tutorials if you search "magento 2 create module" in Google

10

Let's name the module StackExchange_HelloWorld.
you will need these files:

app/code/StackExchange/HelloWorld/registration.php - the registration file

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

app/code/StackExchange/HelloWorld/etc/module.xml - the module declaration file

<?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="StackExchange_HelloWorld" setup_version="2.0.0" />
</config>

app/code/StackExchange/HelloWorld/etc/frontend/routes.xml - the frontend routing file

<?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="StackExchange_HelloWorld" />
        </route>
    </router>
</config>

app/code/StackExchange/HelloWorld/Controller/Index/Index.php - the index controller

<?php 
namespace StackExchange\HelloWorld\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action
{
     protected $resultPageFactory;
     public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    )
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
         $resultPage = $this->resultPageFactory->create();
         $resultPage->getConfig()->getTitle()->set(__('Hello World'));
         return $resultPage;
    }
}

app/code/StackExchange/HelloWorld/view/frontend/layout/helloworld_index_index.xml - the layout file

<?xml version="1.0"?>
<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" template="StackExchange_HelloWorld::index.phtml" />
        </referenceContainer>
    </body>
</page>

app/code/StackExchange/HelloWorld/view/frontend/templates/index.phtml - the template for the block

 <h2>Hello World</h2>

after you are done, run this in the console

php bin/magento setup:upgrade

You should be able to see the result at the url [ROOT]/helloworld

6
  • Thanks @Marius. I have tried this. It is not working. Shows 404 error. Commented Jun 23, 2016 at 5:03
  • is your module enabled? Does it appear in system->configuration ?
    – Marius
    Commented Jun 23, 2016 at 7:24
  • yes. enabled and showing in system->configuration. Commented Jun 23, 2016 at 7:26
  • OK. i will try again and edit the answer if needed.
    – Marius
    Commented Jun 23, 2016 at 7:26
  • 1
    I think I found the problem. The routes.xml file should be placed in app/code/StackExchange/HelloWorld/etc/frontend not in app/code/StackExchange/HelloWorld/frontend. There was an etc missing. And I forgot 2 use clauses in the controller file. See my update answer.
    – Marius
    Commented Jun 23, 2016 at 7:36
1

I also just tried yesterday and succeeded to make my own hello world magento 2 module. I followed this tutorial to create a simple Magento 2 module, there are 6 steps as below

=> Step 1: Make module folder:

app/code/Magentoexplorer/Helloworld

=> Step 2: Add module.xml to declear the module

    <?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="Magentoexplorer_Helloworld" setup_version="1.0.0" />
</config>

=> Step 3: Create registration.php to register the module

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

=> Step 4: How to Install, Enable or Disable/remove the module

cd [magento_directory]
php bin/magento setup:upgrade

=> Step 5: Route of the module. create app/code/Magentoexplorer/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 id="magentoexplorer" frontName="helloworld">
            <module name="Magentoexplorer_Helloworld" />
        </route>
    </router>
</config>

=> Step 6: Controller and action.

app/code/Magentoexplorer/Helloworld/Index/Index.php

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

class Display extends \Magento\Framework\App\Action\Action
{
  public function __construct(
\Magento\Framework\App\Action\Context $context)
  {
    return parent::__construct($context);
  }

  public function execute()
  {
    echo 'Hello World';
    exit;
  }
}
0

The best module for you to follow would be this one: https://github.com/magento/magento2-samples/tree/master/sample-module-newpage

It's focused on the frontend for Magento 2. You can maybe use this module and transform it into your own module.

0

The most bare module is easy enough:

  • Inside app/code create your folder for Vendor and Module. I.e. app/code/MyCompany/FirstModule
  • Inside the FirstModule folder add a registration.php

    DIR );

  • Inside the same folder, create an etc folder, like app/code/MyCompany/FirstModule/etc

  • In the etc folder create a module.xml

And voila. That's it. You can now activate your module via SSH with the bin/magento module:enable MyCompany_FirstModule command.

0

Below is the tutorial for simple module

https://www.mageplaza.com/magento-2-module-development/

You can also download below module

https://github.com/tzyganu/Magento2SampleModule

There are lots of module creator available for Magento 2. Here are some links

http://cedcommerce.com/magento-2-module-creator/

https://amasty.com/magento-2-module-creator.html

Hope it helps :)

0

To create module in Magento 2, folowing steps are required:

  1. Create directories.
  2. Configuration of Module
  3. Registration of Module
  4. Front End Router File
  5. Create Controller
  6. Create Block
  7. Frontend layout File
  8. Frontend Template File
  9. Module Activation
0

To create Hello World module in Magento 2.

For Best Use PHP Storm

To create Hello World module, you need to complete the following high-level steps:

Best Practice Use PHP Storm

Step 1: Create the folder of Hello World module

Step 2: Create module

Step 3: Register the Created Module

Step 4: Enable the module

Step 1: Create Folder of HelloWorld

Name of the module is defined as “VendorName_ModuleName”. First part is name of the vendor and last part is name of the module: For example: Sathya_HelloWorld.

##### create file directory as

Magento2/app/code/Sathya/HelloWorld

Step 2: Create Module

it is necessary to create etc folder and add the module.xml file
app/code/Sathya/HelloWorld/etc/module.xml

Contents would be:

<?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="Sathya_HelloWorld" setup_version="1.0.0">
   </module>
</config>

Step 3 : Register the Created Module

Create Registration.php file

 app/code/Sathya/HelloWorld/registration.php

Contents would be:

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

Step 4: Enable the module

Before enable the Module make sure whether the Module is created or not. For that run the command from the Magento root directory as.

 php bin/magento module:status

It Lists all the Disabled Modules

###### Sathya_HelloWorld

To enable the Module run the command as:

 php bin/magento module:enable Sathya_HelloWorld
Also there is an another way to enable it. Which will be explained later.

Please upgrade your database: Run “bin/magento setup:upgrade” from the Magento root directory.

Let run the command:

  php bin/magento setup:upgrade

Please run

  php bin/magento setup:static-content:deploy

Then run (optional)

  php bin/magento setup:static-content:deploy -f

To add route, it is necessary to create routes.xml file

app/code/Sathya/HelloWorld/etc/frontend/routes.xml

Content would be:

<?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="Sathya_HelloWorld"/>
        </route>
    </router>
</config>

The directory and file you need to create is:

app/code/Sathya/HelloWorld/Controller/Index/Test.php

Contents would be:

<?php
namespace Sathya\HelloWorld\Controller\Index;

class Test 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()
    {
        echo "Hello World";
        exit;
    }
}

After completed, please run command to clear cache

php bin/magento c:f

Check your Module by entering URL now should be as:

 http://< YourDomain.com >/helloworld/index/test
0

Here is an easy example to start with custom module. https://learn2code2web.com/2020/12/14/magento-2-custom-module-development/

0

To create a "Hello, World!" module in Magento 2, you will need to follow these steps:

Create a module directory structure: First, you will need to create a directory structure for your module. The recommended structure is as follows:

app/code/Vendor/Module/

├── composer.json ├── etc │ ├── module.xml ├── registration.php

Vendor and Module should be replaced with your own vendor and module names, respectively. The composer.json file is used to manage dependencies for your module. The etc directory will contain the module.xml file, which is used to declare your module to Magento. The registration.php file is used to register your module with Magento. Create the module.xml file: In the etc directory, create a file named module.xml with the following contents:

<?xml version="1.0"?>

Replace Vendor and Module with your own vendor and module names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.