Skip to content
Magento glossary

What is Magento di.xml?

Short answer di.xml configures Magento's dependency injection: which implementation or configured value is supplied when an object is constructed, and which plugins apply to eligible methods. Modules can declare global and area-specific configuration. It helps to distinguish a constructor dependency, a preference and an interceptor before choosing the XML node for a change.

How it works
  1. Constructor types describe dependencies 01
  2. Configuration is merged for an area 02
  3. Arguments customize a particular type 03
  4. Preferences and plugins change different things 04
  5. Compilation prepares deployment artifacts 05
Each step is explained below

Watch for this Choosing a scope that cannot affect the request

How it works

From constructor dependencies to effective DI configuration

Start with the class and request area, then choose the configuration mechanism. Generated code is one part of deployment, rather than proof that the feature behaves correctly.

  1. 01

    Constructor types describe dependencies

    A class asks for its collaborators through constructor parameters. Magento's object manager resolves those parameters using type information and DI configuration. You do not need an explicit XML entry for every ordinary constructor dependency. Keep business code dependent on suitable contracts instead of obtaining arbitrary services from the object manager.

  2. 02

    Configuration is merged for an area

    The application loads initial, global and area-specific DI configuration. A file in etc/frontend/di.xml applies to the storefront area; etc/graphql/di.xml applies to GraphQL. Put a change in the scope where it belongs. Adobe lists the configuration stages and argument types.

  3. 03

    Arguments customize a particular type

    A <type> node can configure named constructor arguments, including scalar and array values. The argument name must match the constructor parameter. A <virtualType> gives an existing type a distinct configured identity without creating a new PHP subclass. Reference that identity where the customized instance is needed.

  4. 04

    Preferences and plugins change different things

    A <preference> maps an abstraction or type to an implementation. A plugin intercepts an eligible method on a configured type. Preferences can have a wide effect and can conflict with another replacement. Plugins also have restrictions and ordering concerns; neither is an automatic answer for every customization.

  5. 05

    Compilation prepares deployment artifacts

    setup:di:compile generates eligible classes and optimizes DI configuration. It does not convert XML into ordinary application source, and a successful compile does not prove that a plugin runs in the correct area. Review generated-code handling through the deployment process and test the actual feature after deployment.

When to use

Choose the declaration that fits the change

Arguments, preferences, plugins and virtual types solve different construction or extension problems. Review the consumers affected by each choice.

  • A service needs a specific constructor value

    Use type arguments when a collaborator needs a configured scalar, array or alternate object. Check parameter names and XML types. Keep environment-specific secrets in the appropriate deployment configuration rather than putting credentials into a module's tracked XML.

  • An interface needs an implementation

    A preference can bind a contract to the class that provides it. Check the existing preference and the consumers affected before changing it. A replacement must remain compatible with the expected constructor and public behavior.

  • An eligible method needs an extension point

    A before, after or around plugin can adjust suitable public method behavior without copying the original class. Choose the narrowest hook that serves the requirement and test the method signature, ordering and result. Read the plugin entry for its restrictions.

  • One class needs two configurations

    A virtual type can reuse a class with different arguments under a distinct name. It is a DI configuration identity, not a PHP class to instantiate or extend directly. Make its consumers explicit so the configuration remains understandable.

Common mistakes

DI mistakes worth checking first

A valid XML file can still target the wrong area or replace more behavior than intended. Inspect the effective configuration and exercise the relevant request.

  • Choosing a scope that cannot affect the request

    A storefront-only declaration will not configure a GraphQL or Admin request. Confirm the entry point and its active area before moving XML around. Test the real request path as well as reading the file; a valid XML document can still be in the wrong place.

  • Changing a preference without reviewing its reach

    A preference can replace the implementation used by multiple consumers. Check for another module configuring the same target, verify compatibility and run relevant regression checks. Module load order does not make incompatible replacements safe to combine.

  • Using compilation as the only acceptance check

    Compilation can catch some construction and generation problems, but it cannot prove business behavior, authorization or frontend interaction. Include focused feature tests and the affected request area. Do not blindly delete generated directories or disable shared caches to make an error disappear.

FAQ

Magento di.xml, frequently asked questions

  • What is di.xml used for in Magento?
    It configures how Magento constructs and extends objects. Common uses include constructor arguments, preferences, plugins and virtual types. Ordinary typed constructor injection does not require a separate XML declaration for every parameter. The effective configuration depends on the module set and request area.
  • What is the difference between a preference and a plugin?
    A preference chooses an implementation for a type or abstraction. A plugin intercepts an eligible method call. They have different compatibility risks and limits. Prefer the narrow mechanism that fits the behavior, and test it against the installed module set rather than treating either approach as universally safer.
  • What is a virtual type?
    A virtual type is a named DI configuration based on an existing type, usually with different arguments. It avoids writing a PHP subclass solely to change construction. It is not a standalone PHP class, so reference it through configuration instead of attempting to instantiate it directly in application code.
  • Where should I put area-specific DI configuration?
    Use the module's etc/<area>/di.xml, such as etc/frontend/di.xml or etc/graphql/di.xml. Use global etc/di.xml when the declaration should apply across areas. Review Adobe's area configuration guidance for the load stages.
  • Does setup:di:compile prove my configuration works?
    No. It generates eligible classes and prepares optimized DI configuration, but a correctly constructed object can still do the wrong thing or be configured in the wrong area. The compilation guide explains the command; validate the resulting feature separately.
  • Why is my plugin not running?
    Confirm the plugin is enabled in the request's area, targets the correct type and method, and respects interception restrictions. Then inspect the effective configuration, generated-code deployment and other plugins. A final, static or non-public method cannot be treated like an ordinary interceptable public method.
di.xml audit

Reviewing a module's DI configuration?

Share the Magento version, the relevant constructor or method and the behavior you need to change. The extension-development page describes how to scope the work.

Kishan Savaliya Online 10 AM to 7 PM IST Chat on WhatsApp