Search code examples
phpyii2yii2-advanced-app

Default main.php config file for all projects in Yii2


I am developing multiple websites based on Yii2 framework and I was wondering if there is any good way or if it's even good idea or not to create a shared basic config main.php file.

The reason behind this is that all my projects are based on same system, have a lot of similarities and I simply want to keep things DRY. Configs are also mostly same - the only differences actually are database names and few other values. Even urlManager rules are not so different.

The problem is that when for example I would want to add additional FileTarget to logger targets for all websites I have to do this for each project separately and I can never be sure if all project configs are up to date and have everything set up properly.

So I was wondering if there is any Yii way of creating default config file shared in multiple projects or if it's simply a bad idea. For example I could create bootstrap component which sets defaults or add default config as first item in the list of merged configs in frontend\web\index.php file. I have private packages added in composer so it's not a problem adding file that will be shared in all projects.


Solution

  • As a simpler alternative you can just put a file somewhere and include it from all the configs i.e.

    $commonConfig = require '/var/www/common/config.php';
    $appConfig = require 'configs/main.php';
    $config = array_merge($commonConfig, $appConfig);
    // ...