Hey, need help with your Magento, WooCommerce or Laravel project? I have some availalility. Contact me.

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',
      ),
    ),
),
),
MageDispatch.com (ad)

Hey, I'm running a developer focussed newsletter called MageDispatch.com, which is for the community and by the community. Here you can share links that you think that the community should know about. If you like this kind of technical content you will like this newsletter too.

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?