How to create a Module in Magento 2

Here I’m going to explain step-by-step how you can create a module in Magento 2 (A module is called component in Magento 2). In Magento 1 we used to write business logic, layout, template and module enabling code in different places but Magento 2, gives us flexibility to create everything in one folder i.e app/code.

I’ve uploaded a simple Magento 2 module on github which will be taken as a reference in this tutorial. If you have any question, leave in the comments section and I will be happy to answer.

Magento 2 has introduced the concept of composer which is helpful to manage components details, components installation, and helps with managing component dependency.

Let’s start creating a new component. Create a folder app/code/Amit/Helloworld/ in root Magento directory, where Amit is the vendor name and Helloworld is the component/module name.
Now inside Helloworld folder create file composer.json with following code.

{
  "name": "amit/helloworld-module",
  "description": "Simple Magento 2 module that shows uses of Controller, Block & Template.",
  "type": "magento2-module",
  "version": "1.0.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0",
    "magento/framework": "~100.0"
  },
  "autoload": {
    "files": [ "registration.php" ],
    "psr-4": {
      "Amit\\Helloworld\\": ""
    }
  }
}

Explanation of the tags defined in composer.json
1. name is the unique name of your component.
2. description describes about your component.
3. type is the type of component.
There are 3 component types:
Module (defined as magento2-module)
Theme (defined as magento2-theme)
Language (defined as magento2-language)
4. version defines version of the component.
5. license defines license type of the component.
6. require defines dependency modules. All modules on which this component will be dependent will be listed here.
7. autoload defines which files should be auto-loaded when this component is installed.

Next, create a file registration.php php with following code

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

Using Magento’s ComponentRegistrar’s register function we are registering our component.
create a file etc/module.xml. It is used to enable our component.

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

Controllers in Magento 2
A frontend controller should be created at app/etc/frontend/routes.xml with following code.

 <?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="Amit_Helloworld" />
        </route>
    </router>
</config>

Where router id is standard which means this controller should be applied on storefront, route id should be a unique id and front name is defined in frontName keyword which is used to access your controller like http://example.cpm/helloworld/ .

Now create your controller file at app/code/Controller/Index/Index.php with following content:

<?php
namespace Amit\Helloworld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }
    public function execute()
    {
       echo "Hello World!";
     }
 }

Now clear var/cache, var/page_cache and var/generation folders and in your browser run your controller like http://example.cpm/helloworld/ and you should see output Hello World! .

Leave a comment if you’re having issue making it work. In next tutorial I’ll be explaining how to create and use layout, block and template.

Advertisements