Edit in GitHubLog an issue

Module dependencies

A software dependency identifies one software component's reliance on another for proper functioning. A core principle of the Adobe Commerce and Magento Open Source framework (Commerce framework) architecture is the minimization of software dependencies. Instead of being closely interrelated with other modules, modules are optimally designed to be loosely coupled. Loosely coupled modules require little or no knowledge of other modules to perform their tasks.

Each module is responsible for a unique feature. In practice, this means that:

  • Several modules cannot be responsible for one feature.

  • One module cannot be responsible for several features.

  • Module dependencies on other modules must be declared explicitly. You must also declare any dependency upon other components (for example, a theme, language package, or library).

  • Removing or disabling a module does not result in disabling other modules.

Dependency types#

There are two types of module dependencies: hard and soft.

Hard dependencies#

A module with a hard dependency on another module cannot function without the module it depends on. These modules:

  • Contain code that directly uses logic from another module, such as class constants, static methods, public class properties, interfaces, and traits.
  • Contain strings that include class names, method names, class constants, class properties, interfaces, and traits from another module.
  • Deserializes an object declared in another module.
  • Uses or modifies the database tables used by another module.

The require section of app/code/<Vendor>/<Module>/composer.json file contains hard dependency definitions for the module. For example:

Copied to your clipboard
1 ...
2 "require": {
3 "magento/module-catalog": "103.0.*",
4 "magento/module-email": "101.0.*",
5 "magento/module-media-storage": "100.3.*",
6 "magento/module-store": "101.0.*",
7 "magento/module-theme": "101.0.*",
8 "magento/module-ui": "101.1.*",
9 "magento/module-variable": "100.3.*",
10 "magento/module-widget": "101.1.*",
11 "magento/module-authorization": "100.3.*"
12 }
13 ...

Soft dependencies#

A module with a soft dependency on another module can function properly without the other module, even if it has a dependency on the other module. These modules:

  • Directly check another module's availability.
  • Extend another module's configuration.
  • Extend another module's layout.

The suggest section of app/code/<Vendor>/<Module>/composer.json file contains soft dependency definitions for the module. For example:

Copied to your clipboard
1 ...
2 "suggest": {
3 "magento/module-graph-ql": "*",
4 "magento/module-graph-ql-cache": "*",
5 "magento/module-store-graph-ql": "*"
6 }
7 ...

The <sequence> node of app/code/<Vendor>/<Module>/etc/module.xml file also contains soft dependency definitions for the module. For example:

Copied to your clipboard
1 <module name="Magento_Cms">
2 <sequence>
3 <module name="Magento_Store"/>
4 <module name="Magento_Theme"/>
5 <module name="Magento_Variable"/>
6 </sequence>
7 </module>

Module install order#

The Commerce framework installs modules in the following order:

  1. The module serving as a dependency for another module
  2. The module dependent on it

Appropriate dependencies#

Although the Commerce framework architecture favors loosely coupled software components, modules can contain dependencies upon these software components:

  • other modules

  • PHP extensions

  • libraries (either Commerce framework library or third party libraries)

Inappropriate dependencies#

Avoid creating these dependencies:

  • Circular (both direct and indirect)
  • Undeclared
  • Incorrect

Dependencies between modules in different presentation layers#

You can build dependencies between the modules belonging to different layers.

Dependencies in the Framework layer#

A module belonging to the Commerce framework can be used in the application layer by an explicit dependency.

Dependencies in the application layer#

A module belonging to the application layer cannot be used in the Commerce framework.

You can build dependencies between classes in the application layer, but these classes must belong to the same module. Dependencies between the modules of the application layer should be built only by the service contract or the service provider interface (SPI).

Managing module dependencies#

At a high level, there are three main steps for managing module dependencies:

  1. Name and declare the module in the module.xml file.

  2. Declare any dependencies that the module has (whether on other modules or on a different component) in the module's composer.json file.

  3. (Optional) Define the desired load order of config files and .css files in the module.xml file.

Example: Module A declares a dependency upon Module B. Thus, in Module A's module.xml file, Module B is listed in the <sequence> list, so that B's files are loaded before A's. Additionally, you must declare a dependency upon Module B in A's composer.json file. Furthermore, in the deployment configuration, Modules A and B must both be defined as enabled.


Copied to your clipboard
1<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
2 <module name="Module_A" setup_version="1.0.0">
3 <sequence>
4 <module name="Module_B" />
5 </sequence>
6 </module>

After installing the module and opening app/etc/config.php, you are able to see that the Module_B was loaded before Module_A:

Copied to your clipboard
1return [
2 'modules' => [
3 ...
4 'Module_B' => 1,
5 'Module_A' => 1,
6 ...
7 ]
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.