Developing First Magento 2 Module

In this article I’m going to discuss how to setup a basic magento 2 module with simple routing. Let’s do this with practical work. Ns_Email module is implemented in order to continue this discussion with practical work. Bellow is the necessary steps to set up a module structure (Note : Magento Enterprise 2.1.3 version is used to creating this module)

Creating Folder Structure

If you are used to develop magento 1 modules, you may familiar with codepools which resides in app/code folder. There are no more codepools in magento 2. Modules are grouped by namespace and placed directly in the app/code folder. So, lets create following folder structure first

  1. app/code/Ns
  2. app/code/Ns/Email
  3. app/code/Ns/Email/etc

The Ns folder is the module’s namespace, and Emial folder is the module’s name.

Note: code folder may not have in magento fresh installation inside app folder. If so, please create it manually.

Now we have the folder structure ready for our module.

Module Declaration file

Magento 2 looks for configuration information for each module in that module’s etc directory. module.xml configuration file is created in etc directory in order to provide those information.


and content for that file would be…

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

With this file we register our module with Ns_Email and set the version as 1.0.0

Registering the module with registration.php

All magento 2 modules needs to be registered through magento 2 ComponentRegistrar class. registration.php class will be created at module’s root directory


and the content for that file would be…


Enable the module

By finishing above steps an empty module is created. It can be tested whether this module has recognized by magento, by running bellow magento shell command (Note: navigate to magento root before you run the magento shell command)

./bin/magento module:status


php bin/magento module:status

if you have not set classpath properly.

If everything has done properly, bellow message will be appeared at the end of the resulting text

List of disabled modules:

Magento confirms that the setup module is recognized by the Magento and is ready to install by giving above result. Following shell command will be used to install the built module

php bin/magento module:enable Ns_Email

Following result will be appeared on command line, if everything ran properly

The following modules have been enabled:
- Ns_Email

Or else, if you run


it will enable all the modules which are not installed

Create a Router

An URL in magento2 can be identified in 4 specific parts.

< BASE_URL>/< router>/< controller>/< action>

let me explain it by an example – BASE_URL
module_example – router
hello_world – controller
say_hello – action

Router in Magento 2 specifies the controller which should called when a certain URL is accessed. routes.xml is used to specify the routers in magento 2

<p>And the content for above file is</p>
<p>So far we have achieved up to this part of the URL</p>

Create Controller and Action

The next aim is to display a simple message on the browser. Let’s assume the URL is Therefor the controller file should be


The content of the above file will be as bellow

namespace Ns\Email\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ResponseInterface;

class SayHello extends Action

     * Dispatch request
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
     * @throws \Magento\Framework\Exception\NotFoundException
    public function execute()
        echo "Hello World";

Well, everything is ready now. If all the above mentioned steps are followed correct, “Hello World” message should be displayed on browser if URL is accessed. Caches will have to be removed prior to access the URL. Caches can be flushed by running following console command

php bin/magento cache:flush