Contents

Creating module in Magnento 2

In this blog post, I’ll guide you through the process of creating an example empty module for Magento 2. This module will be used as scaffolding for my other modules/articles. Thats why I decided to create separate tutorial on how to prepare it.

Placement of the module

The easiest way to add module is to just add it to app/code. You can also use composer to install the modulus, but if you already know how to do it, this tutorial is probably not for you.

In app/code you need to create a new directory looking like this: VendorName/ModuleName

Name of my module will be: Wyganowski/Example

In app/code/VendorName/ModuleName (in my case app/code/Wyganowski/Example) you need to create a two files. The first one is registration.php which register the module to Magento framework. The second one is module.xml in the etc directory of your module. This file is used to provide essential metadata for your module. The structure of your module should looks like this:

VendorName/ModuleName
|-- registration.php
|-- etc
|  |-- module.xml

registration.php

registration.php file should look like this:

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Wyganowski_Example',
    __DIR__
);

module.xml

module.xml file placed in VendorName/ModuleName/etc in empty module should look like this:

<?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="Wyganowski_Example" setup_version="1.0.0" />
</config>

Installing the module

After adding these files, you need to run this command in your terminal to install the module:

bin/magento setup:upgrade

Check if your module is installed

To check if your module is installed properly you can check status of it running this command:

bin/magento module:status Wyganowski_Example

Sum up

If you did everything correctly, you should now have successfully created your custom module. I’ve set up a repository with my example module for reference. You can check it here here.

TL;DR

Download this module, put it in your app/code and change Wyganowski_Example to VendorName/ModuleName in registration.php,etc/module.xml.