2

I’m currently working on a symfony project for our company. And I have issues figuring out how to build a dynamic configuration from an environment variable.

My main problem here is that symfony caches my “defaut_database” once, and then never changes it. I’m willing to change this with a variable defined in each Vhost I create.

Here is my Vhost configuration :

<Macro demoVHost ${slug}>
<VirtualHost *:80>
   ServerName ${slug}.domain.com
   DocumentRoot /home/domain/www/web

   SetEnv SYMFONY__DYNAMIC__DATABASE_NAME db_${slug}

   <Directory /home/domain/www/web>
        AllowOverride None
        Order Allow,Deny
        Allow from All
        Require all granted

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app_demo.php [QSA,L]
        </IfModule>
   </Directory>

   ErrorLog /home/demo/log/${slug}.error.log
   CustomLog /home/demo/log/${slug}.access.log combined
</VirtualHost>
</Macro>

Use demoVHost aaa
Use demoVHost bbb
Use demoVHost ccc

And my .yml configuration :

doctrine_mongodb:
   connections:
       default:
           server: 'mongodb://127.0.0.1:27017'
           options: {}
   default_database: "%dynamic.database_name%"
  • The way I do it is by having multiple `prod` environments and separate parameters files for them (for setting database, user, host etc..) that way you can separate your cache, logs, uploads and what not. Provide some kind of parameter that you can access say from $SERVER in php and load your configuration in AppKernel depending on it. For example `foo` results in `bar` environment, so load `foo` parameters. Execute console commands providing environment using `--env=foo`. If you have furher questions, tommorow I might provide some code for you, as this is just the general idea. Hope it helps. – Laurynas Jul 16 '18 at 19:24

2 Answers2

0

You can use different environments to use with different databases. So every environment will generate own container cache file with preserved database connection settings. All environments can use shared APC cache for all project files but use own settings.

To make it possible you don't need any .htaccess you just need to edit your app.php:

$domainParts = explode('.', $_SERVER['SERVER_NAME']);
if (count($domainParts) === 3) { // If you have domains like aaa.domain.com, bbb.domain.com and so on
    $instance = $domainParts[0];
} else { // If user request "not right" domain like aaa.aaa.domain.com or domain.com
    $instance = 'prod';
}

$loader = new ApcClassLoader('your-apc-prefix-to-have-all-files-shared-across-all-instances', $loader);
$loader->register(true);


require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';

$kernel = new AppKernel($instance, false);

Then you need to create appropriate config files with names app/config/config_aaa.yml for domain aaa.domain.com, app/config/config_bbb.yml for domain bbb.domain.com and so on. Every config file contains database configuration and import for main config.prod file:

imports:
    - { resource: config_prod.yml }

doctrine_mongodb:
   default_database: 'aaa' # or 'bbb', 'ccc' — for concrete domain

Common for all environments config you can place in config_prod.yml:

doctrine_mongodb:
   connections:
       default:
           server: 'mongodb://127.0.0.1:27017'
           options: {}
Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
0

My main objective was to allow the creation of an infinite number of demo platforms, on a single environment, but with a dedicated database for each one - and automatically, not manually.

domain_AAA => app_demo.php/config_demo.php => db_AAA
domain_BBB => app_demo.php/config_demo.php => db_BBB
domain_CCC => app_demo.php/config_demo.php => db_CCC

Hence, to resolve my issue, i just thought about creating an additional level in my cache directory for my “demo” environment, and it worked perfectly.

In my AppKernel.php file.

public function getCacheDir()
{
    if ($this->getEnvironment() === 'demo') {
        return dirname(__DIR__).'/var/cache/'.$this->getEnvironment() . '/' . $this->getEnvParameter('subdomain');
    }
    return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
}

public function getEnvParameter($name)
{
    $envParameters = $this->getEnvParameters();
    return array_key_exists($name, $envParameters) ? $envParameters[$name] : null;
}