Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

« Previous Version 3 Next »

Timeline: 2h

Module is a structural element of Magento 2 – the whole system is built upon modules. Typically, the first step in creating a customization is building a module.

To create a module, you need to complete the following high-level steps:

  1. Create the module folder.

  2. Create the etc/module.xml file.

  3. Create the registration.php file.

  4. Run the bin/magento setup:upgrade script to install the new module.

  5. Check that the module is working.

Let’s go through each of these steps in detail.

Create the module folder

Let’s create the folder app/code/Learning and inside this folder place another folder: FirstUnit. If you're using the command line, the code would be:

  1. cd to the root folder

  2. mkdir app/code/Learning

  3. mkdir app/code/Learning/FirstUnit

Using the following command-line code, create the folder app/code/Learning/FirstUnit/etc:

mkdir app/code/Learning/FirstUnit/etc

Then put the following code into it:

<?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="Learning_FirstUnit" >
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Note that in the XML file we specified:

  • Module name: Learning_FirstUnit (based on the folders we created)

  • Dependency: Magento_Catalog. We could have multiple dependencies. In this case, we would put <module name=”..” /> nodes under the sequence node.

Create the registration.php file

Each module must have this file, which tells Magento how to locate the module. Continuing our example, create the file app/code/Learning/FirstUnit/registration.php. Then put the following content into it:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use \Magento\Framework\Component\ComponentRegistrar;

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

The registration.php is a standardized file that follows the same pattern for all modules.

The only thing that varies is the module name, which in our case is Learning_FirstUnit.

Run the “setup:upgrade” command

Running this command makes your new module active, notifying Magento of its presence.

php bin/magento setup:upgrade

It should echo a large amount of output, one line of which should be Learning_FirstUnit. Verify that this line of code is there.

Check that the new module is active

So far, we haven't added any useful code to our module – it is still empty (and therefore invisible). In order to verify that it has been recognized, check the file app/etc/config.php. It has a list of auto-generated modules that are active.

Never change this list manually!

grep Learning_FirstUnit app/etc/config.php

Employing these steps, you can successfully create a new module in Magento 2

  • No labels