DEV Community

Richard
Richard

Posted on

Module Development in Magento 2 (Adobe Commerce): Registering a Module

Intro

I've been getting involved in Magento 2 (Adobe Commerce) module development for a project at work.

I'm not particularly versed in Magento 2 but I've been developing for a few years now and I've learnt that I can't resist picking up something new and the inevitable challenge that ensues.

While working on this project I've found that the best Magento 2 resources are behind paywalls and the offical documentation, while extensive, is not easy to digest.

The aim of this series of posts is to cover a few aspects of Module development in the Magento 2 ecosystem and how to use a Module to make customisations to a store.

Why

I find that I don't feel like I fully understand something unless I can explain it to someone else.

Assumptions

  • You have an install of Magento 2 that you can develop with (if you don't then this repo with docker will work wonders.
  • You've heard of and know some basics of XML, PHP, HTML etc

Where do I develop Modules

My first mistake was assuming that I should develop my module immediately as a vendor file. Magento module development should always be made in the app/code folder. This allows you to easily tweak and edit the module without having to run too many special commands.

You'll eventually be able to package up the module for distrubution, but development is best in this location.

Useful Commands

  • bin/magento setup:upgrade : Registers and reinitialises modules
  • bin/magento cache:flush : Clears the various caches to force changes

File Structure

Inside the app/code folder you'll need to place your module within a namespace folder. Typically this will be your company name. This name needs to be as unique as possible to avoid conflict with every other company name making modules for Magento 2. A company name is usually sufficient.

Within your namespace folder you'll have your module folder. And within the module folder you'll have a few different folders and files that handle various things. You won't need all of these, depending on what you want to achieve, but it's good to know what's possible.

If you'd rather read the official docs, here is the link!

Companyname [namespace]
-- Modulename [module name]
---- Api 
---- Block [Content block creation]
---- Controller
---- Console
---- etc
------ adminhtml [config for modifying/adding functionality]
-------- di.xml
-------- events.xml
-------- menu.xml
-------- routes.xml
-------- system.xml
------ frontend [config for modifying/adding functionality]
-------- di.xml
-------- events.xml
-------- page_types.xml
-------- routes.xml
------ webapi_rest
-------- di.xml
------ webapi_soap
-------- di.xml
------ acl.xml
------ config.xml
------ di.xml
------ eav_attributes.xml
------ module.xml
------ webapi.xml
---- Helper [For reuseable code across the module]
---- i18n
---- Model
---- Plugin [For hooking into functions]
---- view [holds templates]
---- composer.json
---- registration.php [registers the module]
Enter fullscreen mode Exit fullscreen mode

Registering the module

You won't need all of the above files to just get the module registered. All you need for this is the following.

Companyname [namespace]
-- Modulename [module name]
---- etc
------ module.xml
---- registration.php
Enter fullscreen mode Exit fullscreen mode

The contents of the module.xml file will need to be something like the below.

<?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="Companyname_Modulename" />
</config>
Enter fullscreen mode Exit fullscreen mode

And the contents of the registration.php file will need to match it:

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Companynamespace_Modulename',
    __DIR__
);
Enter fullscreen mode Exit fullscreen mode

Once you have this in place you can run this command using the Magento CLI

bin/magento setup:upgrade
Enter fullscreen mode Exit fullscreen mode

Your module will now be registered and active. However it doesn't really do anything yet, it's just working.

What next?

You'll now be able to start adding files and folders and functionality to the module to make it suit your needs. Within this module you can pretty much change anything and everything in Magento 2.

In my next article I'll look at adding additional functionality to the settings files and adding additional items to the admin menu.

If you do venture ahead on your own, remember that you will need to clear the cache between updates, and occasionally recompile.

Thanks for reading my learning notes!

Top comments (0)