Save environment specific settings in your env.php file

Magento 2, Magento 2 local environment

This scenario has happened to me too many times: I downloaded a backup of a site (stripped from customer data of course), import the database in my local environment, and open the site. At this moment you will get redirected to the site where the data originates from and you figure you need to change some URLs and flush the cache of your browser. I hate that.

Luckily does Magento provide us with a mechanism that prevents us from this: You can set configuration variables in your env.php. If you import a new database into your environment, Magento will notice that the settings aren't up to date and will show an error that you need to import them before continuing. This way you never initialize a session that redirects you to the wrong store. And the good thing is: It also works for other settings, API keys for example.

The default env.php

If you open up your env.php that gets generate while installing Magento, it will look something like this:

<?php
return array (
'backend' => array (
      // ...
),
'crypt' => array (
      // ...
),
'db' => array (
      // ...
),
'resource' => array (
      // ...
),
'x-frame-options' => 'SAMEORIGIN',
'MAGE_MODE' => 'developer',
'cache_types' => array (
      // ...
),
'install' => array (
      // ...
),
);

To set the correct URLs add them like this:

'system' =>
array (
'default' =>
array (
    'web' =>
    array (
      'unsecure' =>
      array (
        'base_url' => 'https://app.mylocalenvironment.test/',
        'base_link_url' => 'https://app.mylocalenvironment.test/',
        'base_static_url' => 'https://app.mylocalenvironment.test/static/',
      ),
      'secure' =>
      array (
        'base_url' => 'https://app.mylocalenvironment.test/',
        'base_link_url' => 'https://app.mylocalenvironment.test/',
        'base_static_url' => 'https://app.mylocalenvironment.test/static/',
      ),
    ),
),
),

Do you have a multistore setup? That is also covered. Instead of default, use the store codes like this:

'system' =>
array (
'football_store' =>
array (
    'web' =>
    array (
      'unsecure' =>
      array (
        'base_url' => 'https://football.mylocalenvironment.test/',
        'base_link_url' => 'https://football.mylocalenvironment.test/',
        'base_static_url' => 'https://football.mylocalenvironment.test/static/',
      ),
      'secure' =>
      array (
        'base_url' => 'https://football.mylocalenvironment.test/',
        'base_link_url' => 'https://football.mylocalenvironment.test/',
        'base_static_url' => 'https://football.mylocalenvironment.test/static/',
      ),
    ),
),
'soccer_store' =>
array (
    'web' =>
    array (
      'unsecure' =>
      array (
        'base_url' => 'https://soccer.mylocalenvironment.test/',
        'base_link_url' => 'https://soccer.mylocalenvironment.test/',
        'base_static_url' => 'https://soccer.mylocalenvironment.test/static/',
      ),
      'secure' =>
      array (
        'base_url' => 'https://soccer.mylocalenvironment.test/',
        'base_link_url' => 'https://soccer.mylocalenvironment.test/',
        'base_static_url' => 'https://soccer.mylocalenvironment.test/static/',
      ),
    ),
),
),

Want to change other settings? Use the same configuration path. Tip: Install ScopeHint2 to find the path quickly. In this example, I'm disabling SMTP so no e-mails get send accidently:

'system' =>
array (
'default' =>
array (
    'system' =>
    array (
      'smtp' =>
      array (
        'disable' => '1',
      ),
    ),
),
),
MageTested.com (ad)

Do you want your Magento store to be more reliable? Tired of things that suddenly break the checkout process without anyone noticing? You can hire my services to kickstart End-2-End testing for your Magento store. This way you know for sure that your store is behaving as expected before releasing that new feature or that update.

View MageTested.com for more information.

Going deeper

For the env.php this is more than enough. But Magento offers way more. You can, for example, do the same trick in the config.php. That file, if you do it correctly, gets committed in your repository, and this way you will set configuration on deployment right away. So no longer: "Warn me when the deployment is complete so I can set the right settings". You can even create new stores using the config.php file. This is out of the scope of this article, but Magento has some docs on how to this, which you can find here:

https://devdocs.magento.com/guides/v2.4/config-guide/deployment/pipeline/example/shared-configuration.html

Want to respond?