Utilize configuration XML and variables scope.

Utilize configuration XML and variables scope.
Magento 2 Exam Series - Magento Architecture & Customization Techniques: Determine how to use configuration files in Magento.

Published by Rick Daalhuizen

Introduction

Much of the configuration in Magento 2 is done by xml files, and placed in the [Module_Root]/etc/ directory. Depending on the scope of your configuration, place them in etc/adminhtml, etc/frontend or under global scope under etc/. When you put them in adminhtml or frontend, this overwrites the global scope.

List of XML config files.

  • acl.xml - resource title, sort
  • adminhtml/rules/payment_{country}.xml - paypal
  • address_formats.xml
  • address_types.xml - format code and title only
  • cache.xml - name, instance - e.g. full_page=Page Cache
  • catalog_attributes.xml - catalog_category, catalog_product, unassignable, used_in_autogeneration, quote_item *
  • communication.xml
  • config.xml - defaults
  • crontab.xml - group[], job instance, method, schedule *
  • cron_groups.xml *
  • di.xml - preference, plugins, virtual type *
  • eav_attributes.xml - locked entity attributes (global, unique etc.)
  • email_templates.xml - id label file type module – view/frontend/email/name.html
  • events.xml - observers, shared, disabled *
  • export.xml
  • extension_attributes.xml - for, attribute code, attribute type
  • fieldset.xml
  • import.xml
  • indexer.xml - class, view_id, title, description
  • integration.xml
  • integration/api.xml
  • integration/config.xml
  • menu.xml - admin menu
  • module.xml - version, sequence
  • mview.xml - scheduled updates, subscribe to table changes, indexer model
  • page_types.xml
  • payment.xml - groups, method allow_multiple_address
  • pdf.xml - renders by type (invoice, shipment, creditmemo) and product type
  • product_types.xml - label, model instance, index priority, (?) custom attributes, (!) composable types
  • product_options.xml
  • resources.xml
  • routes.xml
  • sales.xml - collectors (quote, order, invoice, credit memo)
  • search_engine.xml
  • search_request.xml - index, dimensions, queries, filters, aggregations, buckets
  • sections.xml - action route placeholder -> invalidate customer sections
  • system.xml - adminhtml config
  • validation.xml - entity, rules, constraints -> class
  • view.xml - vars by module
  • webapi.xml - route, method, service class and method, resources
  • widget.xml - class, email compatible, image, ttl (?), label, description, parameters
  • zip_codes.xml - Add custom input mask for ZIP code

Configuration load order

The load order of the configuration files start with app/etc/di.xml.First it’s collecting configration files under [Module_Root]etc/*.xml, then under [Module_Root]/etc/[Area]/*.xml, and finally merges them together.

While merging the configuration files, it is checked whether the id attribute of an xml node is unique, otherwise it will be overwritten with all underlying content (child nodes).

When the same xml document (configuration file) contains multiple nodes with the same id, it will give an error. – Magento DevDocs - Module configuration files

  1. Config merger class
    • \Magento\Framework\Config\Dom
  2. When merging each file
    • createConfigMerger|merge
    • \Magento\Framework\Config\Dom::_initDom(dom, perFileSchema)
    • \Magento\Framework\Config\Dom::validateDomDocument
    • $dom->schemaValidate
  3. After everything is merged
    • \Magento\Framework\Config\Dom::validate(mergedSchema)
    • \Magento\Framework\Config\Dom::validateDomDocument

Environment settings

Magento 2 comes with a configuration file app/etc/env.php where you can store system-specific but also sensitive files, these can be API keys, passwords and personal information e.g. email or phone number. Sensitive data is not included during the export.

This file also contains your db data, crypt key to protect your passwords and other sensitive information (created during the Magento installation), backend frontName for your admin url and cache types.

To create a configuration you can do this as follows with bin/magento.

To see more options run bin/magento config, options for Magento 2.2.4 or higher may differ.

Set non-sensitive configuration

bin/magento config:set [--scope] [--scope-code] path value

Set sensitive configuration (writes to app/etc/env.php)

bin/magento config:sensitive:set [--scope] [--scope-code] path value

Show saved configuration

bin/magento config:show

Export configuration, note: does not include sensitive data!

bin/magento app:config:dump

Example

bin/magento config:set --scope=websites --scope-code=base web/unsecure/base_url http://example.com/

To find the right scope you can run the following sql query in your db:

SELECT * FROM store;
SELECT * FROM store_website;

With magerun:

n98-magerun.phar db:query "SELECT * FROM store;"
n98-magerun.phar db:query "SELECT * FROM store_website;"

For more information, you can consult the following Magento documentation