Reading list Switch to dark mode

    Magento 2 Development 04: Module Registration

    Updated 3 August 2023

    Introduction

    In this blog, we will learn how to create and register a Module in Magento.

    In Magento, a module is a standalone unit where we write our functionalities or business logic to achieve our goal.

    It is a group of directories that contain the blocks, controllers, helpers, and models needed to create a specific store feature. It is the unit of customization in the Magento platform.

    Magento modules can be created to perform multiple functions, from influencing user experience to changing storefront appearance. They can install, deleted, or disabled.

    Steps to Create a Module

    • Create the module folder.
    • Create the registration.php file.
    • Create the module.xml file.
    • Run the command: php bin/magento setup:upgrade
    • Run the command: php bin/magento setup:di:compile

    Create the module Folder

    Now to create the module we have to create a vendor folder first with the vendor name (which is Webkul in our case) under the app/code folder.

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    Selection_123

    (if the code folder is not available then please create it).

    Then we have to create the module folder “BlogManager” (the module name).

    Selection_121-1
    Folder Structure

    Now our folder structure should be like (app/code/Webkul/BlogManager). Where Webkul is our Vendor name and BlogManager is our Module name.

    Create the registration.php file

    After this, we have to create a registration.php file under the file path (app/code/Webkul/BlogManager) so that Magento can recognize it as a module and register it.

    Code for app/code/Webkul/BlogManager/registration.php file

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Webkul_BlogManager',
        __DIR__
    );

    Types of components are,
    i) Module
    ii) Theme
    iii) Language
    iv) Library

    Selection_124
    Folder structure

    Create the module.xml file.

    To create the module.xml file, we first need to create an etc folder under the directory (app/code/Webkul/Blogmanager), and in the etc folder we need to create a module.xml file with the following content.

    Code for etc/module.xml file

    <?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="Webkul_BlogManager">
        </module>
    </config>
    Selection_125
    Folder structure
    Selection_127
    For better understanding

    Run the command: php bin/magento setup:upgrade

    After creating these two files we need to run the setup upgrade command like below in the terminal at the Magento root folder. It will activate the module.

    php bin/magento setup:upgrade
    Selection_076-1

    This command is used to update the Magento database schema and data, as well as apply any necessary system upgrades. It is used in the Magento 2 upgrade process and ensures that your Magento store runs smoothly with the latest updates and features.

    Run the command: php bin/magento setup:di:compile

    It is used to compile code like Factories, Proxies, Interceptors, etc., and puts them in the generated directory.

    php bin/magento setup:di:compile
    Selection_077-1

    Folder Structure till now

    Selection_126
    Folder Structure

    Next blog -> Magento 2 Development 05: Route Management and Controllers

    Previous blog -> Magento 2 Development 03: Area codes

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home