Describe Module Structure
Understanding the essential architecture and requirements of a Magento module.
Module Structure Overview
Module Location and Naming
A Magento 2 module is generally found in the app/code/VendorName/ModuleName directory.
Directory Structure
- Custom modules: app/code/VendorName/ModuleName
- Third-party modules: vendor/(installed via Composer)
vendor/ directory, not app/code.
    Module Identity
A module's identity involves two parts:
1️⃣ Module Name
The name Magento uses to recognize and identify the module.
Format: VendorName_ModuleName
2️⃣ PSR-4 Path
The namespace path used in code, used by Composer to autoload files.
Format: VendorName\ModuleName
When to Create a New Module
You should create a new module in these scenarios:
Create a New Module When:
- Adding new features with significant code (estimated >250 lines)
- Customizing functionality in a different existing Magento module
- Creating a supporting framework for a new child theme (e.g., MerchantName/Theme)- Contains ViewModels, LESS, and Layout XML
 
- Grouping general customizations by area of functionality (e.g., MerchantName/Catalog)
- Modifying a third-party module (e.g., MerchantName/VendorModuleName)
Required Module Files
A custom module requires several mandatory files in its directory:
1. registration.php
Purpose:
- Used by Composer to determine which files to autoload based on namespace path
- Used by Magento to understand which modules are available in the system
Example:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Bonlineco_Blog',
    __DIR__
);2. composer.json
Purpose:
- Primarily used by Composer to define requirements and paths
- Must specify typeas"magento2-module"
- The autoloadsection specifies PSR-4 path mapping
Example:
{
    "name": "bonlineco/module-blog",
    "description": "Blog module for Bonlineco store",
    "type": "magento2-module",
    "version": "1.0.0",
    "require": {
        "php": "~8.1.0||~8.2.0",
        "magento/framework": "*"
    },
    "autoload": {
        "psr-4": {
            "Bonlineco\\\\Blog\\\\": ""
        },
        "files": [
            "registration.php"
        ]
    }
}- "type": "magento2-module"identifies this as a Magento module
- PSR-4 mapping: "Bonlineco\\\\Blog\\\\": ""maps to current directory
- Files array includes registration.phpfor autoloading
3. etc/module.xml
Purpose:
- Must specify the module's name, matching the name in registration.php
- The <sequence>element defines modules this module depends on
- If a module is in the sequence, Magento won't allow it to be disabled if this module is enabled
Example:
<?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="Bonlineco_Blog" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Cms"/>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>- If Magento_Cmsis listed in sequence, it cannot be disabled whileBonlineco_Blogis enabled
- This ensures all required modules are active
- The sequence also controls the order in which modules load
Steps to Add a New Module
After creating the directory and populating the mandatory files, perform these command-line actions:
Installation Steps
Step 1: Enable the Module
bin/magento module:enable Bonlineco_BlogStep 2: Install/Synchronize
bin/magento setup:upgradeWhat setup:upgrade does:
- Synchronizes the database schema
- Runs any necessary setup scripts
- Installs the module into the database (visible in setup_moduletable)
--keep-generated
        bin/magento setup:upgrade --keep-generatedPurpose:
- Prevents deletion of the generated/directory
- Speeds up the development process
- ⚠️ Do NOT use if your module modifies:
                - Extension attributes
- Plugins
 
- If unsure, manually delete generated/directory to ensure changes take effect
Module Directory Structure Example
Complete Module Structure
app/code/Bonlineco/Blog/
├── registration.php         (Required)
├── composer.json           (Recommended)
├── etc/
│   ├── module.xml         (Required)
│   ├── di.xml
│   └── acl.xml
├── Block/
├── Controller/
├── Model/
├── Setup/
├── view/
│   ├── frontend/
│   └── adminhtml/
└── i18n/Quick Reference
| File | Required? | Purpose | 
|---|---|---|
| registration.php | Required | Registers module with Magento and Composer | 
| composer.json | Recommended | Defines dependencies and PSR-4 autoloading | 
| etc/module.xml | Required | Module name and dependency sequence | 
Exam Tips
Key Points to Remember
- Know the difference between Module Name (Vendor_Module) and PSR-4 Path (Vendor\Module)
- Understand the 3 required files and their purposes
- Remember that <sequence>in module.xml controls dependencies and load order
- Know when to use --keep-generatedflag (and when NOT to use it)
- Understand the criteria for creating a new module (>250 lines, customizing other modules, etc.)
- Remember that composer.jsonmust have"type": "magento2-module"
- Know the two-step process: module:enablethensetup:upgrade
