Create Your First Magento Module

Product

Want to learn everything about M2?

Quickly learn and reference all Magento programming basics, concepts & patterns, even with no prior experience.

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 Macademy vendor name and the Jumpstart module name. Typically a module describes what it does; for example, if we were creating functionality for a blog, we would just name it Blog. Since we’ll just be creating some test code, Jumpstart works since it’s related to this course.

Within our app/code folder, let’s create a folder with our vendor name, which is Macademy. And then within that directory, let’s create another one with the name of our module, Jumpstart. 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/Macademy/Jumpstart

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 flexible 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/Macademy/Jumpstart/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.

Again, don’t worry about remembering this file. Even after programming in Magento for years, I still refer back to other modules for examples of these files.

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="Macademy_Jumpstart"/>
</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/Macademy/Jumpstart/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,
    'Macademy_Jumpstart',
    __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 Macademy_Jumpstart

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:

php 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. Don't worry if it feels overwhelming; remember, we're learning it step by step.

Complete and Continue  
Extra lesson content locked
Enroll to access all lessons, source code & comments.
Enroll now to Unlock