Using system.xml in Magento 2
What is system.xml in Magento 2?
system.xml is used in Magento 2 to create configuration settings in the Admin Panel → Stores → Configuration. It allows developers to add custom configuration fields such as text inputs, dropdowns, toggles, and more for module settings.
These configuration values are stored in the core_config_data database table and can be accessed programmatically in Magento using configuration APIs.
Why Use system.xml?
- Create configurable settings for custom modules.
- Allow admin users to control module behavior.
- Store configuration values per scope (Global, Website, Store).
- Provide flexible module configuration without code changes.
Step 1: Create system.xml
This file defines configuration sections, groups, and fields.
(File: app/code/VendorName/ModuleName/etc/adminhtml/system.xml)
Configuration Structure Explained
- section → Main configuration category.
- group → Sub-section inside section.
- field → Actual configuration input field.
- tab → Determines which admin tab the section appears under.
- resource → Controls ACL permissions.
Configuration path format:
Example:
Step 2: Add ACL Permission
Magento requires ACL permissions to control access to configuration settings.
(File: app/code/VendorName/ModuleName/etc/acl.xml)
Step 3: Read Configuration Value in Code
Configuration values can be retrieved using ScopeConfigInterface.
Where Configuration Values are Stored
Magento saves configuration values in the database table:
Columns:
- scope → default / websites / stores
- scope_id → ID of scope
- path → configuration path
- value → saved value
Best Practices
- Always use meaningful section and field IDs.
- Use ACL to control admin access.
- Define configuration constants for paths.
- Cache configuration for performance.
- Use scope-specific values when needed.