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.

app/code/Ns/Email/etc/module.xml

and content for that file would be…

<?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="Ns_Email" setup_version="2.0.0">
    </module>
</config>

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

app/code/Ns/Email/registration.php

and the content for that file would be…

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

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

or

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:
Ns_Email

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

setup:upgrade

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

https://example.com/module_example/hello_world/say_hello

https://example.com – 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

app/code/Ns/Email/etc/frontend/routes.xml1
<p>And the content for above file is</p>
1
<p>So far we have achieved up to this part of the URL</p>
1http://www.example.com/nsmail/

Create Controller and Action

The next aim is to display a simple message on the browser. Let’s assume the URL is http://www.example.com/nsmail/index/sayhello. Therefor the controller file should be

app/code/Ns/Email/Controller/index/SayHello.php

The content of the above file will be as bellow

<?php
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";
        exit();
    }
}

Well, everything is ready now. If all the above mentioned steps are followed correct, “Hello World” message should be displayed on browser if http://example.com/nsmail/index/sayhello 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