What registration.php Does
The registration.php file is the entry point for Magento's component discovery system. It registers modules, themes, and language packages with the ComponentRegistrar.
Purpose:
- Tells Magento where to find the component
- Registers the component type (module, theme, language)
- Enables Magento to load the component's code
- Provides the component's filesystem path
Module registration:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Theme registration:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::THEME,
'frontend/Vendor/custom_theme',
__DIR__
);
Language package registration:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::LANGUAGE,
'Vendor_Module_fr_FR',
__DIR__
);
How Magento uses registration.php:
- Magento scans
app/code/andvendor/forregistration.phpfiles - Each file is included during bootstrapping
ComponentRegistrar::register()stores the component path- Magento reads
app/etc/config.phpto know which are enabled - Only enabled components are loaded into the application
ComponentRegistrar Internals
The ComponentRegistrar is a registry that stores component paths and types.
ComponentRegistrar class:
<?php
namespace Magento\Framework\Component;
class ComponentRegistrar
{
const MODULE = 'module';
const THEME = 'theme';
const LANGUAGE = 'language';
private static $components = [];
public static function register($componentType, $componentName, $path)
{
if (!isset(self::$components[$componentType])) {
self::$components[$componentType] = [];
}
self::$components[$componentType][$componentName] = $path;
}
public static function getComponent($componentType)
{
return self::$components[$componentType] ?? [];
}
public static function getModuleList()
{
return self::getComponent(self::MODULE);
}
}
Discovery process:
// Magento scans for registration.php files
$componentRegistrar = $objectManager->get(ComponentRegistrar::class);
// After all registration.php files are included:
$modules = ComponentRegistrar::getModuleList();
// Returns: [
// 'Magento_Catalog' => '/path/to/vendor/magento/module-catalog',
// 'Vendor_Module' => '/path/to/app/code/Vendor/Module',
// ...
// ]
Registration file locations:
- Module:
app/code/{Vendor}/{Module}/registration.php - Theme:
app/design/frontend/{Vendor}/{Theme}/registration.php - Language:
app/i18n/{Vendor}/{locale}/registration.php - Composer package:
vendor/{vendor}/{package}/registration.php
Autoloading registration.php:
Composer includes registration.php files automatically via the files autoload directive:
{
"autoload": {
"files": [
"registration.php"
]
}
}
Module Registration Best Practices
Follow these best practices when creating registration.php files.
Standard module registration:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Naming conventions:
- Module name:
Vendor_Module(underscore-separated) - Vendor name: PascalCase (e.g.,
MyCompany) - Module name: PascalCase (e.g.,
ProductFilter)
Verification:
# Check if module is registered
php bin/magento module:status Vendor_Module
# List all registered modules
php bin/magento module:status
Common mistakes:
- Wrong path: Ensure
__DIR__points to module root - Missing ComponentRegistrar import: Add
usestatement - Incorrect component type: Use
ComponentRegistrar::MODULE - Naming mismatch: Name must match module.xml name
Debugging registration issues:
# Check if registration.php is loaded
grep -r "ComponentRegistrar::register" app/code/Vendor/Module/
# Verify module is in config.php
php bin/magento module:status
# Clear config cache
php bin/magento cache:clean config
Module name must match:
// registration.php
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Vendor_Module', // Must match module.xml
__DIR__
);
<!-- module.xml -->
<module name="Vendor_Module" ...>
<!-- Name must match registration.php -->
</module>
Theme and Language Registration
Themes and language packages use the same ComponentRegistrar system with different component types.
Theme registration:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::THEME,
'frontend/Vendor/custom_theme',
__DIR__
);
Theme naming format:
- Frontend:
frontend/{Vendor}/{theme} - Admin:
adminhtml/{Vendor}/{theme}
Language package registration:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::LANGUAGE,
'Vendor_Module_fr_FR',
__DIR__
);
Language naming format:
{Vendor}_{Module}_{locale}- Example:
Magento_FR_frorVendor_Module_de_DE
Listing registered themes:
use Magento\Framework\Component\ComponentRegistrar;
$themes = ComponentRegistrar::getComponent(ComponentRegistrar::THEME);
foreach ($themes as $themePath => $dir) {
echo $themePath . ' => ' . $dir . "\n";
}
Listing registered languages:
$languages = ComponentRegistrar::getComponent(ComponentRegistrar::LANGUAGE);
foreach ($languages as $langCode => $dir) {
echo $langCode . ' => ' . $dir . "\n";
}
Verification commands:
# List all themes
php bin/magento theme:list
# List all languages
php bin/magento language:list
# Check theme status
php bin/magento theme:status
Quiz
1. What does registration.php do?
2. What component type constant is used for themes?
3. What is the naming format for theme registration?
Flashcards
Question
What are the three component types in ComponentRegistrar?
Click to reveal answer
Answer
MODULE, THEME, LANGUAGE
Question
What must match between registration.php and module.xml?
Click to reveal answer
Answer
The module name (e.g., Vendor_Module)
Question
How does Composer include registration.php files?
Click to reveal answer
Answer
Via the files autoload directive in composer.json
Question
What command lists all registered modules?
Click to reveal answer
Answer
php bin/magento module:status
Revision Notes
Key Takeaways
- 1. registration.php registers components with ComponentRegistrar
- 2. Three component types: MODULE, THEME, LANGUAGE
- 3. Module name in registration.php must match module.xml
- 4. Theme format: frontend/{Vendor}/{theme}
- 5. Magento scans for registration.php during bootstrapping
- 6. Composer auto-includes registration.php via files autoload
Interview Tips
- • Explain what registration.php does and why it's needed
- • Describe the ComponentRegistrar registration process
- • Know the naming conventions for modules, themes, and languages
- • Discuss how Magento discovers components at boot time
- • Explain the relationship between registration.php and module.xml
Cheat Sheet
registration.php Cheat Sheet
Module:
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Theme:
ComponentRegistrar::register(
ComponentRegistrar::THEME,
'frontend/Vendor/theme',
__DIR__
);
Language:
ComponentRegistrar::register(
ComponentRegistrar::LANGUAGE,
'Vendor_Module_fr_FR',
__DIR__
);
Verify:
php bin/magento module:status
php bin/magento theme:list
php bin/magento language:list