loading a config from a versioned module.

I have an api application with api/common/config/main.php which gets loaded when the application runs. However, I have api/module/v1/config/main.php configuration that I want to include in the module.php init() method. I tried using \Yii::configure($this, require DIR . ‘/config/main.php’), but that overrides whatever configuration that was previously loaded. What is the best way to accomplish this?

Just include it in index.php. For example:




$config = yii\helpers\ArrayHelper::merge(

    require __DIR__ . '/../../common/config/main.php',

    require __DIR__ . '/../../common/config/main-local.php',

    require __DIR__ . '/../config/main.php',

    require __DIR__ . '/../config/main-local.php',

    // somewhere here

);



Yes, that is how I have it working, but the downside with this approach is I am hard coding the versioning of the API config. I want the correct config for that version of the API to load depending on the url. For example "https://domain.com/api/v1/<controller>/<action>" should load the v1 config and "https://domain.com/api/v2/<controller>/<action>" should load the v2 config.




$config = yii\helpers\ArrayHelper::merge(

    require __DIR__ . '/../../common/config/main.php',

    require __DIR__ . '/../../common/config/main-local.php',

    require __DIR__ . '/../config/main.php',

    require __DIR__ . '/../config/main-local.php',

    require(__DIR__ . '/../modules/v1/config/main.php')

);



I think you could use the "init()" method of the module to dynamically add the module specific config.

https://www.yiiframework.com/doc/guide/2.0/en/structure-modules#module-classes

But, the url manager’s rules for the module must be in the application config.

https://www.yiiframework.com/doc/guide/2.0/en/structure-modules#routes

Thanks @softark, I agree! This is exactly what I tried. My url manager rules and all my major configurations all get loaded fine in my common/config/main.php. I have a few additional parameters in my api/module/v1/config/params.php and my api/module/v1/config/main.php file that are specific to v1 of my API. So I am trying to load them from my module init(). It gets loaded, but all my previous configuration from /common/config/main.php get’s overwritten with the config file from my module.




public function init()

{

    parent::init();

    // initialize the module with the configuration loaded from config.php

    \Yii::configure($this, require __DIR__ . '/config/main.php');

}



Hmm, it seems to be the expected result. The module specific config SHOULD overwrite the common one.

(Maybe I don’t understand what you mean …)