Magento 2 Create a Module

Creating a custom module in Magento 2 allows developers to extend or add new functionality. This guide explains how to create a basic module from scratch using best practices compatible with Magento 2.4.8.

📁 Step 1: Create the Module Directory

Navigate to your Magento root directory and create the following folder:

app/code/Bcn/HelloWorld/
  

📝 Step 2: Create registration.php

This file registers your module with the Magento framework.

// app/code/Bcn/HelloWorld/registration.php
use Magento\Framework\Component\ComponentRegistrar;

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

📝 Step 3: Create module.xml

This configuration file declares the module name and version.




    

  

⚙️ Step 4: Enable the Module

Run the following CLI commands to enable and register the module:

php bin/magento setup:upgrade
php bin/magento module:enable Bcn_HelloWorld
php bin/magento cache:flush
  

✅ Verify the Module

Check the module status to ensure it’s active:

php bin/magento module:status Bcn_HelloWorld
  

📌 Summary

  • 📦 Magento modules are stored under app/code/.
  • 🧾 registration.php and module.xml are mandatory.
  • ⚙️ Use CLI commands to enable and install the module.

You’re now ready to add features like routes, controllers, models, and blocks to this module!