Magento2 – HelloWorld – Module Development Part1

In this blog post we will see how to create simple “hello world” module in magento2.

In magento2 all modules reside in the folder app/code, previously in magento1 there was the concept of local/ community/ core/ folders but that has been removed now.
In this blog post we will see how to create a new module, create a route and display “hello world”

Step1

Module names in magento2 are divided into two part “VendorName_ModuleName”
e.g Magento_Contact, Magento_Catalog or Excellence_Test
first part is the vendor and second part is the actual module.

Let’s take our module name to be “Excellence_Hello”. First we need to make the folders

app/code/Excellence/Hello

Step2 – module.xml

Next we need to add the module.xml file

app/code/Excellence/Hello/etc/module.xml

contents are

<?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="Excellence_Hello" setup_version="0.0.1"/>
</config>

Step3 – registration.php

Next need to add a registration.php

app/code/Excellence/Hello/registration.php

content would be

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

Step4

Now an empty module is ready, we now need to enable it.

At this stage if you run the command

php bin/magento module:status

You should see

List of disabled modules:
Excellence_Hello

This means the module is setup, but it is disabled right now.

To enable the module, run the command

php bin/magento module:enable Excellence_Hello

this should enable your module.
Another way of doing this is, go to the file

app/etc/config.php

you will see a long list of modules there, just add your module as well

...
'Excellence_Hello' => 1,
....

this should enable your module as well.

After this step, when you open your website in browser you will get an error saying

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

run the command

bin/magento setup:upgrade

to fix this. At this point you should be all set with a empty module.
You can also see your module at System Configuration -> Advanced -> Disable Modules Output

Step5 – Routes

Now lets add a route (or url) for our module so that we can display “hello world”
Route’s in magento are divided into 3 parts

http://mymagento.com/index.php/route_id/controller/action

index.php is optional and depends on your magento configuration. If you have .htaccess file work index.php is not required.

To add route we need to add routes.xml file

Excellence/Hello/etc/frontend/routes.xml

since this is a frontend route, we added it in frontend/ folder else we need to add it to adminhtml/ folder

the content of the file are

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="excellence" frontName="excellence">
            <module name="Excellence_Hello" />
        </route>
    </router>
</config>   

We usually keep id and frontName the same, or it might causes some issues.
Here we defined the first part of our route. So till now our route is

www.mymagento.com/excellence/* 

Next we need to define our controller,action.
Lets assume we want our url to be

www.mymagento.com/excellence/hello/world

for this we need to create the following folder

Excellence/Hello/Controller/Hello/World.php

and add the following content in

World.php

file

<?php
namespace Excellence\Hello\Controller\Hello;


class World 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 you have followed all steps properly and open the url in browser you see the output “Hello World”

Important
1. There is another important thing to note, if you miss out controller or action name it automatically defaults to Index. Meaning, a url like www.mymagento.com/excellence would find path
Excellence/Hello/Controller/Index/Index.php
2. Another important thing, magento create auto generated files at var/generation/Excellence/Hello/Controller . So if your noticing that, your making changes to controller and the changes are not showing up. Make sure to delete the cache file generated.

Try Out this yourself to make sure you understood above
1. Create a url like /excellence_test/hello_world/test
2. Create a url like /excellence/world
3. Create a url like /excellence

  • Thank’s 😀

  • Step to Step information about the magento – module development. That is impressive,. Thanks a ton 🙂

  • charley

    Hi, “In magento2 all modules reside in the folder app/code” I’ve just downloaded magento 2.0.9 CE and there is no app/code. What is the equivalent directory structure?

    • Manish Prakash

      if don’t have app/code folder then you can create app/code folder it should work ( some version don’t have code folder )

  • Priyansh Agrawal

    i have tried all your steps to create “hello world ” plugin but it shows error (regarding routing ,i think) :-

    There has been an error processing your request

    Exception printing is disabled by default for security reasons.

    Error log record number: 1781955897

    • Priyansh Agrawal

      what to do ??

    • Manish Prakash

      go to var/report folder , open 781955897 this file and check what the exact error

  • jaimin

    Hi Manish,

    i have tried your exercise and i have successfully done 2 and 3rd but first i have tried by the way of magento 1 by creating sub folder but it gives error so can you tell us perfect way for doing the first exercise?

    • Satyendra

      similar problem for execrecise 1. Any solution ?

      • Satyendra

        I got the solution. You need to create world folder inside hello and then test

  • Guru

    Hi,
    I created the module in my localhost; When i am trying to access the extension by below route
    http://localhost:88/magento/index.php/excellence/hello/world
    I am getting The page you requested was not found
    Please can you help me how to solve this.
    Thanks
    Ponguru

    • Manish Prakash

      Hello Guru, after module creation is done run the command : “php bin/magento module:status” in magento root directory to check that the module Excellence_Module is enabled or not. If its not enabled, then you need to run following commands:
      php bin/magento module:enable Excellence_Hello
      php bin/magento setup:upgrade

      In case your module is enabled and still getting error message, then check file & directory permission.

      • Guru

        Thanks for your reply.
        In step5 i made a mistake instead of routes.xml i saved it as router.xml
        After i renamed to routes.xml , Its starts working

  • Guru

    Hi,
    In step5 i made a mistake instead of routes.xml i saved it as router.xml
    After i renamed to routes.xml , Its starts working
    Thanks for the post