Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel configuration file structure

We are using same code base for multiple sites. And there are will be config files which are both site specific and common settings. We want to merge the common settings with site specific settings.

Our config folder structure is -

app/config -

  • ssc/
  • brg/
  • app.php
  • common.php

and so on. ssc and brg will hold site specific configs for ssc & brg sites respectively.

And for this we have to do -

    config('common');
    config('brg.settings');

But in our code base we have not used the sub-folder mechanism. When we will be implementing the sub-folder system then we have to make changes to the code base to implement the site specific folder which I want to avoid.

Is there any way to implement the sub-folder system and dont have to changes the code base?

like image 440
Sougata Bose Avatar asked Nov 08 '22 05:11

Sougata Bose


1 Answers

In a multi-tenant environment

You can merge the settings into the common config inside a middleware or at the point you know which site you are in:

config('common');
config('brg.settings'); // inside each have the same keys
config('ssc.settings'); // inside each have the same keys

So lets say you want common.settings where settings comes from either brg.settings or scc.settings you can try something like the following:

config([
    'common' => array_merge(
        config('brg'), 
        config('common')
    )
]); 

If you want to amend it so that brg.settings become keys inside common try the following:

config([
    'common' => array_merge(
        config('brg.settings'), 
        config('common')
    )
]); 

Update - For a shared (copied) folder approach

Lets say your config folder has:

config/common.php
config/brg/settings.php

Then you could try and create a symlink from config/brg/settings.php to config/settings.php

$ cd config
$ ln -s brg/settings.php settings.php

You might even want to move the brg/settings.php out of the config folder into something like sites/brg/settings/php to avoid laravel building a composite of all the sites config and just use the symlink in config to what you want.

Alternatively

As part of your deployment you can create an artisan command that creates the physical config file you need like, config/site/settings.php given the site inside the .env.

I think it is still good to move the site specific config out of the config folder to avoid Laravel needing to parse the unneeded files.

like image 171
Leon Vismer Avatar answered Nov 14 '22 23:11

Leon Vismer