Agent skill
magento-di
Configure Magento 2 dependency injection — di.xml, types, virtual types, preferences, argument replacement, and Object Manager. Use when wiring dependencies, creating class variations, or configuring module integrations.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/magento-di
SKILL.md
Magento 2 Dependency Injection
Before writing code
Fetch live docs:
- Web-search
site:developer.adobe.com commerce php development components dependency-injectionfor the DI guide - Fetch
https://developer.adobe.com/commerce/php/development/and navigate to DI documentation - Web-search
site:developer.adobe.com commerce php development build di-xmlfor di.xml reference
Conceptual Architecture
How DI Works in Magento
Magento's Object Manager reads di.xml configurations and automatically injects dependencies into class constructors. You declare what you need; the framework provides it.
Constructor injection is the primary pattern — declare dependencies as constructor parameters with type hints.
di.xml Scope
di.xml files are area-scoped:
etc/di.xml— global (all areas)etc/frontend/di.xml— storefront onlyetc/adminhtml/di.xml— admin panel onlyetc/webapi_rest/di.xml— REST API onlyetc/webapi_soap/di.xml— SOAP API only
Types
Configure constructor arguments for a specific class:
- Override default values
- Inject different implementations per area
- Argument types:
string,boolean,number,const,null,object,array,init_parameter
Virtual Types
Create class variations without writing new PHP files:
- Same base class with different constructor arguments
- Only exists in DI configuration
- Cannot be injected by classname directly (use as a
typeattribute value) - Reduces code duplication significantly
Preferences
Map an interface to a concrete implementation:
<preference for="InterfaceName" type="ConcreteClassName" />- Global preference applies everywhere unless overridden by area-specific di.xml
- Foundation of Magento's interface-based programming
Argument Types
| Type | Description |
|---|---|
string |
String value |
boolean |
true or false |
number |
Integer or float |
const |
PHP constant value |
null |
Null value |
object |
Another class instance (injected) |
array |
Array of mixed argument types |
init_parameter |
Value from Magento\Framework\App\DeploymentConfig |
Shared vs Non-Shared
- By default, Object Manager creates shared instances (singleton behavior)
- Set
shared="false"on a type to get a new instance each time - Factories (
SomeClassFactory) always create new instances
Sensitive/Environment Config
init_parameter type reads from app/etc/env.php — use for environment-specific values that shouldn't be in di.xml.
Common Patterns
- Interface → Implementation mapping: preference for repository, data, and service interfaces
- Logger customization: virtual type with custom handler arguments
- Collection modification: type with different filter arguments per area
- Plugin declaration: type with plugin child element (covered in plugins skill)
Best Practices
- Always inject interfaces, not concrete classes
- Use virtual types to avoid unnecessary PHP files
- Scope di.xml to the smallest applicable area
- Never call Object Manager directly in application code (only in factories and framework)
- Use
shared="false"sparingly — most dependencies should be shared
Fetch the DI documentation for exact XML schema, element attributes, and current best practices before configuring.
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?