Module Development in Magento 2 (Adobe Commerce): Registering a Module
Tags: magento, todayilearned, webdev, tutorial
Published: 2022-08-21 20:54:14

## 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](https://m.academy/courses/) are behind paywalls and the [offical documentation](https://experienceleague.adobe.com/docs/commerce.html), 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](https://github.com/markshust/docker-magento) 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!](https://developer.adobe.com/commerce/php/development/build/component-file-structure/)

```
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]
```

## 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
```

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

```xml
<?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>
```

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

```php
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Companynamespace_Modulename',
    __DIR__
);
```

Once you have this in place you can run this command using the Magento CLI
```
bin/magento setup:upgrade
```
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!