If you use Drupal 8's configuration system, then you know that one of the trickiest parts of using it effectively is managing configuration on a per-environment basis. Luckily, the Config Split module makes it easy to manage different configurations in different environments, but how to set it up properly isn't always readily apparent.
In this blog post, I'll provide one method for setting up Config Split in an efficient manner, with splits for local, remote development, and production environments.
The goal is to be able to set things up right the first time, without having to worry about manually enabling/disabling different "splits" depending on the environment.
The key to it all is leveraging Config Split's ability to enable/disable individual splits with configuration in the settings.php file.
The first step is to set up your splits - I normally start by doing this on my local environment. Once Config Split is enabled, via the admin toolbar, head to Configuration | Development | Configuration Split Settings (/admin/config/development/configuration/config-split). Click to add a new split setting - in this example, we'll use the following settings:
- Label: Local
- Folder: ../config/splits/local
- Active: (selected)
- Modules: Complete Split: Devel, Devel Generate, Devel Kint, Reroute emails
- Configuration items: reroute_emails.settings
Let's also add a second split - this one for remote development environments:
- Label: Dev
- Folder: ../config/splits/dev
- Active: (selected)
- Modules: Complete Split: Reroute emails
- Configuration items: reroute_emails.settings
Note that we're not going to split out the Config Split configurations (meta, I know), rather we're going to allow the Local and Dev Config Split settings to be exported to the main /config/sync/ directory.
Next, when exporting config, the following directories and files will be created:
/config/splits/local/ reroute_email.settings.yml /config/splits/dev/ reroute_email.settings.yml /config/sync/ ... config_split.config_split.local.yml config_split.config_split.dev.yml ...
At this point, if we did nothing else, then both the "Local" and "Dev" splits would be automatically active all the time in all the environments and we really wouldn't have accomplished anything yet.
The final step to make this all work is to add a couple of lines of code to each environment's settings.php file. For example, in the settings.local.php, add:
$config['config_split.config_split.local']['status'] = TRUE; $config['config_split.config_split.dev']['status'] = FALSE;
Then, in the settings.php for the remote development environments, add:
$config['config_split.config_split.local']['status'] = FALSE; $config['config_split.config_split.dev']['status'] = TRUE;
If you use a single settings.php for all environments, then you can use if-statements to determine the current environment and then enable/disable the appropriate splits. For example:
if ($env == 'local') { $config['config_split.config_split.local']['status'] = TRUE; $config['config_split.config_split.dev']['status'] = FALSE; elseif ($env == 'dev') { $config['config_split.config_split.local']['status'] = FALSE; $config['config_split.config_split.dev']['status'] = TRUE; else { $config['config_split.config_split.local']['status'] = FALSE; $config['config_split.config_split.dev']['status'] = FALSE; }
With this type of Config Split setup, you'll always be able to immediately run a configuration import after pulling in new commits from the project repository without having to worry about manually enabling or disabling your splits.
Comments
"if ($env == 'dev') {" ==>…
"if ($env == 'dev') {" ==> "elseif ($env === 'dev') {"
Yikes! Good catch! Fixed :)
Yikes! Good catch! Fixed :)
How do you handle necessary…
How do you handle necessary changes in the .htaccess files?
That's not really a…
That's not really a configuration issue. But, to answer your question, I'd use the new Drupal core-composer-scaffold plugin - see https://github.com/drupal/core-composer-scaffold#altering-scaffold-files