8

I'm new to magento2.

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

How to create this module?

  • 2
    Sounds to me like someone can't use a search engine or is too lazy to do so. – Julien Lachal Jun 23 '16 at 8:01
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

  • Thanks @Marius. I have tried this. It is not working. Shows 404 error. – Saravanan DS Jun 23 '16 at 5:03
  • is your module enabled? Does it appear in system->configuration ? – Marius Jun 23 '16 at 7:24
  • yes. enabled and showing in system->configuration. – Saravanan DS Jun 23 '16 at 7:26
  • OK. i will try again and edit the answer if needed. – Marius Jun 23 '16 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 Jun 23 '16 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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