Mitech Preloader
Magento

How to create a simple Helloworld module in Magento 2

simple_helloworld_module_in_magento_2
In this post we will create a simple Helloworld module in Magento 2. Fisrt of all you should know that in magento 2 there are no more code pools.Modules are grouped by namespace and placed directly in the app/code folder. Now let’s start!

Here is folders we will nedd to create this Module.
helloworld_folders

Step 1 : Open your app folder and create the following folders

If you don’t have code folder in your app directory, create it manually.

  • app/code/Webcreta
  • app/code/Webcreta/Helloworld

Here Webcreta is your namespace and Helloworld is you module’s name

Step 2 : Create a module.xml file in the app/code/Webcreta/Helloworld/etc folder with the following code:

[php]
<?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=”Webcreta_Helloworld” setup_version=”1.0.0″>
</module>
</config>
[/php]

Step 3 : To register our module, create a registration.php file in the app/code/Webcreta/Helloworld folder with the following code:

[php]
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
‘Webcreta_Helloworld’,
__DIR__
);
[/php]

Step 4 : Open cmd and go to the Magento 2 root. Run this command:

[php]
php bin/magento setup:upgrade
[/php]

Let’s make sure that our module is installed,To check that you can go to Admin → Stores → Configuration → Advanced → Advanced and check that the module is present in the list or you can open app/etc/config.php and check the array for the ‘Webcreta_Helloworld’ key, whose value should be set to 1.

Step 5 : To create controller first we need to create routes.xml file in the app/code/Webcreta/Helloworld/etc/frontend folder with the following code:

[php]
<?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=”Webcreta_Helloworld” />
</route>
</router>
</config>
[/php]

Here you can see frontName=”helloworld” so “helloworld” is going to be the first part of our URL.

Step 6 : Create the Index.php controller file in the app/code/Webcreta/Helloworld/Controller/Index folder with the following code:

[php]
<?php namespace Webcreta\Helloworld\Controller\Index; use Magento\Framework\App\Action\Context; class Index extends \Magento\Framework\App\Action\Action { protected $_resultPageFactory; public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory) { $this->_resultPageFactory = $resultPageFactory;
parent::__construct($context);
}

public function execute()
{
$resultPage = $this->_resultPageFactory->create();
return $resultPage;
}
}
[/php]

In Magento 2 every action has its own class which implements the execute() method.

Step 7 : Create a Helloworld.php file in the app/code/Webcreta/Helloworld/Block folder with the following code:

[php]
<?php
namespace Webcreta\Helloworld\Block;

class Helloworld extends \Magento\Framework\View\Element\Template
{
public function getHelloWorldTxt()
{
return ‘Hello world!’;
}
}
[/php]

Next we are going to create layout and template files
In Magento 2, layout files and templates are placed in the view folder inside your module. Inside the view folder, we can have three subfolders: adminhtml, base and frontend.
The adminhtml folder is used for admin, the frontend folder is used for frontend and the base folder is used for both, admin and frontend files.

Step 8 : Create helloworld_index_index.xml file in the app/code/Webcreta/Helloworld/view/frontend/layout folder with the following code:

[php]
<page xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd” layout=”1column”>
<body>
<referenceContainer name=”content”>
<block class=”Webcreta\Helloworld\Block\Helloworld” name=”helloworld” template=”helloworld.phtml” />
</referenceContainer>
</body>
</page>
[/php]

Here you can see template=”helloworld.phtml” so now we will create that file.

Step 9 : Create a helloworld.phtml file in the app/code/Webcreta/Helloworld/view/frontend/templates folder with the following code:

[php]

<h1><?php echo $this->getHelloWorldTxt(); ?></h1>

[/php]

Ok we are done. Now open the http://localhost/magento/helloworld URL in your browser.

I Hope it helps!

blank