We keep diving into Magento 2. Today we are going to make a detailed examination of one of the basic elements of any CMS structure — its configuration. Things a developer can do, ways to save data and increase basic capacities.
Here is a list of questions, reviewed in this article:
- How does the framework discover active extensions and their configuration?
- By what process do the factory methods and autoloader enable class instantiation?
- What are the common methods with which the framework accesses its configuration values and areas?
- How are per-store configuration values established in the XML DOM?
- Which class types and files have explicit paths?
So let’s jump right in and answer these questions one by one.
Methods the framework uses to discover active modules and their configuration
To register module in the system create a file registration.php in the root directory of the module, which contains the following content (by the example of Magento_Catalog module):
This code allows Magento to locate the module and to register it after the usage of bin/magento setup:upgrade.
Modules in Magento 2 can be installed into the directory app/code or using composer. When installing via composer, all the data from registration.php is automatically written in vendor/composer/autoload_files.php. When installing via the directory of app/code all modules registration files are read through the script /app/etc/NonComposerComponentRegistration.php (it’s also loaded in vendor/composer/autoload_files.php), then the information about switched on/off modules is written in app/etc/config.php.
In order to load module version files and its dependencies (module.xml) use the following class: Magento\Framework\Module\ModuleList\Loader and its method load().
All the other configuration files are read using Magento\Framework\Config\Reader\Filesystem and its method read($scope).
Process by which the factory methods and autoloader tenable class instantiation
In Magento 2 autoloader is implemented in vendor/composer/autoload_real.php and uses spl_autoload_register for class instantiation.
Common methods with which the framework accesses its configuration values and areas
The following classes are used for working with the configuration in Magento 2: \Magento\Framework\App\Config\Storage\WriterInterface using the following methods:
save($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
delete($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
Magento\Framework\App\Config\ScopeConfigInterface using method:
getValue($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
A way to establish per-store configuration values in the XML DOM
Per-store configuration is indicated in file etc/config.xml in module directory as follows (example from module Magento_Backend):
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
Class types and files which have explicit paths
The following class types and files have explicit paths:
- Resource Models
That is all we have to share with you for now. We do hope these tips were helpful and you will easily implement them into your work. Follow our new publications to learn more about Magento development.
You can also check out our previous articles about Magento 2 Development.
Still using Magento 1? Click here to learn about smooth migration to Magento 2.