Create new module

YouTube player

Hello everyone

In this lesson we will know how to create new Magento module

Magento Module Naming Convention

The naming conventions for modules in Magento are straightforward. A module name consists of two parts, a “Vendor” name and a “Module” name.

Step 1: Name your module

In this course, we’ll use the Itcforu vendor name and the KickStart module name. Typically a module describes what it does.

Within our app/code folder, let’s create a folder with our vendor name, which is Itcforu. And then within that directory, let’s create another one with the name of our module, KickStart . Note how we capitalize the first letter of each the vendor and module name. This is because casing is very important in all areas of Magento.

app/code/Itcforu/KickStart

Step 2: Create the module.xml File

Next, we will create an etc directory within our module folder. This directory contains XML configuration files for our module. Magento uses this interesting XML approach to make code flexibile and more maintainable, and you’ll find out a bit more about it later. But first, we will need to create a module.xml file within it, which let’s Magento know that our code is in fact module code.

app/code/Itcforu/KickStart/etc/module.xml

It can be hard to remember the exact format of these files, which is why I’m going to use my tool Beeline to generate the root node of this file for me. If you don’t have access to Beeline, just tap the View Source Code button below this lesson to grab the code. You can also copy & paste the contents of another module.xml file into your custom file.

Now, we’ll simply create a new module node. It will have a single attribute, name, which will be set equal to our vendor name and module name, but combined with an underscore. Magento uses this VendorName_ModuleName syntax all throughout it’s internal code to keep track of what’s happening within your module. This name should exactly match the names of your module directories, casing, spelling & all.

<?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="Itcforu_KickStart"/>
</config>

Step 3: Register the module

Finally, the last code we need to create is the registration.php file in the root of our module directory:

app/code/ Itcforu/KickStart/registration.php

The contents of the registration.php file will be:

<?php

declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

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

The call to register() makes our module available on the PHP level, otherwise, Magento won’t even know our module.xml exists, since the PHP layer loads the XML files into configuration.

Enable the module via command line

Now that we have created our Magento module, all we need to do now is enable it from command line with:

bin/magento module:enable Itcforu_KickStart

Since this is a brand new module without any other code, all we need to do is clear the cache with:

bin/magento cache:flush

This will ensure our module is properly registered with Magento. We can confirm this is the case by running the command:

bin/magento module:status

The output of this command should show our module in the list of enabled modules. If it doesn’t, be sure to double-check the spelling and casing of your module name in your directory names and code files.

Congrats! 👏🎉 You’ve just made your first Magento module. But it doesn’t do anything yet besides make itself aware to the Magento codebase.

Each extra piece of code added to our module from this point on carries out very specific functionality.