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”


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


Step2 – module.xml

Next we need to add the module.xml file


contents are

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Excellence_Hello" setup_version="0.0.1"/>

Step3 – registration.php

Next need to add a registration.php


content would be



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:

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


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

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


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="" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="excellence" frontName="excellence">
            <module name="Excellence_Hello" />

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* 

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

for this we need to create the following folder


and add the following content in



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';

If you have followed all steps properly and open the url in browser you see the output “Hello World”

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 would find path
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