Magento2 – HelloWorld – Module Development Part-1

In this blog post we will see how to create a simple hello world module in magento2. In magento2 we do not have code pool, all our module resides in app/code folder. This blog will result in a very basic module that will print text “hello world” through controller.

Step 1

We have name our module as W3solver_Helloworld where W3solver is Namespace and Helloworld is our module name. Similar to magento1 we will create W3solver/Helloworld folder

app/code/W3solver/Helloworld

Step 2

Now, we will register our module by creating registration.php file and adding following code in it.

app/code/W3solver/Helloworld/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE, 'W3solver_Helloworld', __DIR__
); 

Step 3

Create a module.xml file

app/code/w3solver/Helloworld/etc/module.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="W3solver_Helloworld" schema_version="1.0.0" setup_version="1.0.0" >
    </module>
</config>

Step 4

Now your module is ready and we can enable it by running setup:upgrade command

Path_to_magento_directory >php bin\magento setup:upgrade

After executing above command you can assure that your module is enabled by checking in config.php file in app/code/W3solver/Helloworld/etc/config.php, there must be one line added:-

...
'W3solver_Helloworld' => 1,
....

Step 5

Now let’s create a url or route for our module. As in Magento1 routes are divided into 3 parts module_route_id/controllername/actionname
This firstly we have to write code for defining route_id, for that we will create routes.xml, path for route file depend whether we are creating admin or frontend controller. If we are creating frontend controller then we have following path and code

W3solver/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="helloworld" frontName="helloworld">
            <module name="W3solver_Helloworld" />
        </route>
    </router>
</config>

Next, we have to define our controller and action
Suppose our controller name is hello and action is index, so we will create a index.php controller file

W3solver/Helloworld/Controller/Hello/Index.php
<?php
namespace W3solver\Helloworld\Controller\Hello;
  
class Index 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;
    } 
}

If everything is followed correctly then you must be viewing “hello world” text.

http://XXX/helloworld/hello/index

Output

Hello World

1 Comments

Leave a Comment.