How to use config.php file efficiently

config.php-file

Everyone knows that the app/etc/config.php file is used to manage to enable/disable the modules in Magento 2 but it is used for other purposes as well which will reduce our manual work for many developers.

Scopes

It is used to create websites, store groups, and store views with the required information.

What is the use of creating a website/store group/store view from the config.php file?

When we are working with a project which contains multiple websites/store groups/store views and when we have more developers in the team then instead of creating the websites/store groups/store views from the admin panel in each developer system, we can add the code in the config.php file. This file can be pushed to the version control tool from there the developers can pull the code and run the setup:upgrade or app:config:import command which will create the websites/store groups/store views.

'scopes' => [
        'websites' => [
            'admin' => [
                'website_id' => '0',
                'code' => 'admin',
                'name' => 'Admin',
                'sort_order' => '0',
                'default_group_id' => '0',
                'is_default' => '0'
            ],
            'base' => [
                'website_id' => '1',
                'code' => 'base',
                'name' => 'Singapore Website',
                'sort_order' => '0',
                'default_group_id' => '1',
                'is_default' => '1'
            ],
            'thailand' => [
                'website_id' => '3',
                'code' => 'thailand_website',
                'name' => 'Thailand Website',
                'sort_order' => '20',
                'default_group_id' => '3',
                'is_default' => '0'
            ]
        ],
        'groups' => [
            [
                'group_id' => '0',
                'website_id' => '0',
                'name' => 'Default',
                'root_category_id' => '0',
                'default_store_id' => '0',
                'code' => 'default'
            ],
            [
                'group_id' => '1',
                'website_id' => '1',
                'name' => 'Singapore Website Group',
                'root_category_id' => '2',
                'default_store_id' => '1',
                'code' => 'main_website_store'
            ],
            [
                'group_id' => '3',
                'website_id' => '3',
                'name' => 'Thailand Website Group',
                'root_category_id' => '2',
                'default_store_id' => '3',
                'code' => 'thailand_website_group'
            ]
        ],
        'stores' => [
            'admin' => [
                'store_id' => '0',
                'code' => 'admin',
                'website_id' => '0',
                'group_id' => '0',
                'name' => 'Admin',
                'sort_order' => '0',
                'is_active' => '1'
            ],
            'default' => [
                'store_id' => '1',
                'code' => 'default',
                'website_id' => '1',
                'group_id' => '1',
                'name' => 'Singapore Store View',
                'sort_order' => '0',
                'is_active' => '1'
            ],
            'thailand_store' => [
                'store_id' => '3',
                'code' => 'thailand_eng_store',
                'website_id' => '3',
                'group_id' => '3',
                'name' => 'Thailand English Store View',
                'sort_order' => '20',
                'is_active' => '1'
            ],
            'thailand_store_thai' => [
                'store_id' => '4',
                'code' => 'thailand_thai_store',
                'website_id' => '3',
                'group_id' => '3',
                'name' => 'Thailand Thailand Store View',
                'sort_order' => '30',
                'is_active' => '1'
            ]
        ]
    ],

System

It is used to manage the system configuration values.

We can maintain the same system configuration values across all the developers within a team so the developers no need to make the changes manually.
We can define the values in all the scopes(Global, Website, Store view), we will need to add the values inside the particular scope with the config path.
Once the values are defined in the config.php file they can’t be changed from the admin panel. The values which are defined on the config.php file will take the top priority.

'system' => [
        'default' => [
            'dev' => [
                'static' => [
                    'sign' => '1'
                ],
                'js' => [
                    'merge_files' => '1',
                    'move_script_to_bottom' => '0'
                ],
                'css' => [
                    'minify_files' => '1',
                    'use_css_critical_path' => '0'
                ]
            ]
        ],
        'stores' => [
            'thailand_thai_store' => [
                'general' => [
                    'locale' => [
                        'code' => 'th_TH'
                    ]
                ]
            ]
        ],
        'websites' => [
            'thailand_website' => [
                'general' => [
                    'country' => [
                        'default' => 'TH',
                        'allow' => 'TH'
                    ],
                    'locale' => [
                        'timezone' => 'Asia/Bangkok'
                    ]
                ],
                'currency' => [
                    'options' => [
                        'base' => 'THB'
                    ]
                ]
            ]
        ]
    ],

Order of precedence:

The values that we enter for the system configurations can be overridden with the values in env.php. Magento takes the default values in this order

  • config.xml
  • Values in DB (Against a specific path)
  • Config.php
  • Env.php

This means the values that we provide config.xml have the least priority and the values in env.php files take the highest priority, which overrides any values provided in the previous areas.

Modules

It is used to manage the status(enable/disable) of the modules.

When we want to disable all the unnecessary/unused modules from the Magento 2 then instead of disabling the modules from each developer system, we can disable the modules from the config.php file and the file can be shared with all the team members.

The value 1 sets a module as enabled and 0 sets as disabled. Suppose we wanted to disable all the graphQL modules from the Magento installation, the below snippet should do the job.

<?php
  return [
        "modules" => [
            'Vendor_ModuleA' => 1,
            'Vendor_ModuleB' => 0,
            'Vendor_ModuleC' => 1,
            'Vendor_ModuleD' => 0,
            'Vendor_ModuleE' => 1,
            'Magento_GraphQl' => 0,
            'Magento_AuthorizenetGraphQl' => 0,
            'Magento_BraintreeGraphQl' => 0,
            'Magento_GraphQl' => 0,
            'Magento_EavGraphQl' => 0,
            'Magento_CatalogInventoryGraphQl' => 0,
            'Magento_StoreGraphQl' => 0,
            'Magento_CheckoutAgreementsGraphQl' => 0,
            'Magento_CmsGraphQl' => 0,
            'Magento_CmsUrlRewriteGraphQl' => 0,
            'Magento_CatalogGraphQl' => 0,
            'Magento_CustomerBalanceGraphQl' => 0,
            'Magento_CustomerGraphQl' => 0,
            'Magento_DirectoryGraphQl' => 0,
            'Magento_DownloadableGraphQl' => 0,
            'Magento_BundleGraphQl' => 0,
            'Magento_QuoteGraphQl' => 0,
            'Magento_GiftCardGraphQl' => 0,
            'Magento_UrlRewriteGraphQl' => 0,
            'Magento_GraphQlCache' => 0,
            'Magento_GroupedProductGraphQl' => 0,
            'Magento_InventoryGraphQl' => 0,
            'Magento_PaypalGraphQl' => 0,
            'Magento_GiftCardAccountGraphQl' => 0,
            'Magento_RelatedProductGraphQl' => 0,
            'Magento_RewardGraphQl' => 0,
            'Magento_RmaGraphQl' => 0,
            'Magento_SalesGraphQl' => 0,
            'Magento_SendFriendGraphQl' => 0,
            'Magento_ConfigurableProductGraphQl' => 0,
            'Magento_SwatchesGraphQl' => 0,
            'Magento_TaxGraphQl' => 0,
            'Magento_ThemeGraphQl' => 0,
            'Magento_CatalogUrlRewriteGraphQl' => 0,
            'Magento_VaultGraphQl' => 0,
            'Magento_WeeeGraphQl' => 0,
            'Magento_WishlistGraphQl' => 0
        ]
    ]

Configuring the batch size for an indexer process. [On env.php file]

The execution time for the catalog product price indexer is decreased from around 4 hours to less than 2 hours by reducing the batch size from 5000 to 1000. You can experiment to find the ideal batch size. In general, decreasing the batch size in half helps speed up the indexer’s performance.

<?php
return [
    'indexer' => [
        'batch_size' => [
            'cataloginventory_stock' => [
                'simple' => 200
            ],
            'catalog_category_product' => 666,
            'catalogsearch_fulltext' => [
                'partial_reindex' => 100,
                'mysql_get' => 500,
                'elastic_save' => 500
            ],
            'catalog_product_price' => [
                'simple' => 200,
                'default' => 500,
                'configurable' => 666
            ],
            'catalogpermissions_category' => 999,
            'inventory' => [
                'simple' => 210,
                'default' => 510,
                'configurable' => 616
            ]
        ]
    ]
];

Import data from configuration files:

Once we are done with adding all the configurations in the config.php file then we need to run the below command to import the data to the database.

bin/magento app:config:import

These configurations include websites, stores, store views, themes, paths, and values.

Export the configuration:

Once we are done with the configuration changes on the admin panel, we can export the configuration changes to maintain consistent configuration across all the systems.
We need to run the below command.
bin/magento app:config:dump {config-types}

config_types is a space-separated list of various config types to dump.
The available config types are scopes, system, themes, and i18n. If config_types is not mentioned in the command then by default all the system configurations will be dumped.

The following example dumps scopes and system only:

bin/magento app:config:dump scopes system

Once the command is finished then the config.php and env.php file will be updated.

Author

Leave a Reply

Your email address will not be published.

Related Posts

Join 40,000+ Magento pros who receive eCommerce insights, tips, and best practices.