This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join our live AMA on the Future of Page Builders with Brizy's CEO! Register Now →

How to Register Component Using Registration.php File in Magento 2

Updated on May 22, 2023

3 Min Read
Register Component Magento 2

Magento offers many features and customization options, making it a go-to choice for online stores. One of the key features of Magento 2 is its modular architecture, which allows developers to extend the platform’s functionality through the use of components.

To take advantage of this modularity, developers must understand how to register their components with Magento 2. This is done using a registration.php file, which defines the component and its dependencies.

In this article, I will provide a step-by-step guide on registering components using the registration.php file in Magento 2. I will start by setting up a Magento 2 environment, then provide an overview of component registration and the role of the registration.php file.

What Are Components in Magento 2?

In Magento 2, components are modular units of code that contain functionality specific to a particular feature or aspect of an ecommerce store.

Each component is essentially a standalone module that can be developed and maintained independently, making it easy to extend the functionality of Magento 2 or customize it to meet specific business needs.

Components can include anything from frontend design templates and JavaScript files to backend controllers and models. They can also depend on other components or modules, which must be specified during registration.

Types of Components in Magento 2

These are the different types of components in Magento 2.

  1. Module
  2. Theme
  3. Language
  4. Library

Depending on the component type, registration is performed through registration.php by adding to it.

Prerequisites for Component Registration

Before registering a component in Magento 2, a few prerequisites must be met.

First and foremost, you must have a working Magento 2 installation, either locally or on a server. This installation must be configured and set up to meet your needs, including any necessary modules, themes, and configurations.

Here are some prerequisites for component registration in Magento:

  • Basic knowledge of PHP and Magento 2 architecture.
  • Understanding of file structure and naming conventions used in Magento 2.
  • Knowledge of how to create and configure modules in Magento 2.
  • A text editor or Integrated Development Environment (IDE) for creating and editing files.
  • Access to a Command Line Interface (CLI) for executing Magento 2 commands.

Component Registration in Magento 2

Magento components, including modules, themes, and language packages, must be registered in the Magento system through the Magento componentRegister class.

Every component must have a file called registration.php in its root directory. For example, if you create an Image Gallery module and your component’s name is Your-Module, you must create registration.php in your component root.

First, create the file in the directory as: app/code/Cloudways/MyModule/registration.php.

1. Register Module

You can register your Magento 2 module by using this code:

ComponentRegistrar::register(ComponentRegistrar::MODULE, '<YourVendorName_ModuleName>', __DIR__);

Where,

  • <YourVendorName> is the name of the main directory which you created for your module and
  • <ModuleName> is the name of your module.

For example:

use \Magento\Framework\Component\ComponentRegistrar;

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

I used Cloudways_Gallery as my vendor name and Notification as my module name.

2. Register Theme

Register your Magento 2 theme by using this code:

ComponentRegistrar::register(ComponentRegistrar::THEME, '<area>/<yourvendor>/<yourtheme name>', __DIR__);

Where,

  • <area> is the functional area of the module, like the frontend, controller, etc.
  • <yourvendor> is the name of the main directory name you created for your theme, and <yourthemename> is the name of your theme.

For example:

ComponentRegistrar::register(ComponentRegistrar::THEME, 'frontend/Cloudways/m2-theme', __DIR__);

Here I used Cloudways as my vendor name, and m2-theme is my theme name.

3. Register Language Package

Register your Magento 2 language packages by using this code:

ComponentRegistrar::register(ComponentRegistrar::LANGUAGE, '<YourVendorName>_<YourpackageName>', __DIR__);

Where,

  • <YourVendorName> is the name of the main directory name which you created for your language and
  • <YourpackageName> is the name of your package.

For example:

ComponentRegistrar::register(ComponentRegistrar::LANGUAGE, 'magento_sp_sp', __DIR__);

4. Register Libraries

Register any Magento 2 library by using this code:

ComponentRegistrar::register(ComponentRegistrar::LIBRARY, '<vendor>/<library_name>', __DIR__);

Here,

  • <vendor> is the name of the company providing the library.
  • <library_name> is the library name.

Example:

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::LIBRARY, 'magento/framework', __DIR__);

Integrate Register.php With Composer

After creating the registration.php file and creating your component’s composer.json file, invoke your registration.php file in the autoload section of composer.json:

{

"name": "your-vendor/your-component",

"autoload": {

"psr-4": { "AcmeVendor\\BarComponent\\": "" },

"files": [ "registration.php" ]

} 

}

Here is the sample registration.php file:

<?php

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, ‘Cloudways_GalleryNotification’, __DIR__);

?>

Summary

Registering components using the registration.php file is a crucial aspect of developing and customizing Magento 2 ecommerce stores. By following the above steps, you can register your module, theme, library, and your language packages.

Proper registration allows components to be easily integrated into a Magento 2 project and improve code reusability, maintainability, and scalability. Let me know if you have trouble with component registration in the comments section.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Jyotishna Kumari

Jyotishina is the Magento Community Expert at Cloudways and has 4 years of experience in web development. She has worked on e-commerce sites since the turn of the millennium and was working with Magento before version 1 was released. She loves to travel and explore new ideas whenever she finds time. Get in touch with her at [email protected].

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now