A free extra for MODX 2 and MODX 3 brought to you by Maple, the UK's leading MODX experts.
When developing a MODX site locally and deploying to production and staging environments, you often need different system settings for each environment. For example to
- override SMTP details
- point to a different Elasticsearch server
- change the error_reporting level.
This extra allows you to override system settings from the file system. It does this by checking for a file called system_settings.php
in the MODX core/config
directory. If this file exists, it will be read and the system settings with the same keys will be overridden. No settings in the database are changed.
This plugin hooks into the earliest MODX lifecycle events it can. This means that it cannot override settings accessed before the OnMODXInit
event. This includes settings accessed in the core/config/config.inc.php
file.
This plugin requires PHP 7.4+ and is tested with PHP 8.1+.
- Create a new Plugin in the MODX Manager
- Name the plugin SystemSettingsOverride
- Copy the contents of
plugin.php
into the plugin - Set the plugin to run on the following events:
OnMODXInit
OnHandleRequest
pdoToolsOnFenomInit
(optional)
- Save the plugin
Create a new file at core/config/system_settings.php
containing the following:
<?php
return [
'sample_setting_key' => 'setting_value',
'sample_setting_key2' => 'setting_value',
];
Congratulations, you have added your first MODX system_settings! 🥳
You can access them in your MODX site by using the [[++sample_setting_key]]
placeholder or $modx->getOption('sample_setting_key')
syntax.
This file should contain system settings that work for all environments.
For system setting overrides specific to one environment (dev
, staging
, production
), create a file called system_settings.local.php
in the core/config
directory. This file should contain the same PHP structure as system_settings.php
but you only need to include the settings that you want to override.
Example:
<?php
return [
'sample_setting_key' => 'my_dev_environment_value',
];
If you use Git, you need to exclude the local settings from being committed, or they will affect all deployments. Open your .gitignore
file and add the following line:
/core/config/system_settings.local.php
5 years ago I wrote an extra called modx-dotenv that did something similar. It didn't integrate as deeply into MODX as it could have. This extra is a rewrite of that extra. It is much more robust and integrates much more deeply into MODX.
Others have asked for this feature in the past. I hope this extra helps.
Edit: Apparently it does work with MODX3. However I'm not testing it on MODX3 at present.
Original answer: It's because of funding. I write what my clients need. If you need this extra for MODX 3, please get in touch to sponsor development.