0

Is it possible to assign environment variables at runtime from a controller?

I am trying to assign the database name to an environment variable of parameters.yml but I get an exception.

"exception":[{"message":"Impossible to call set() on a frozen ParameterBag."

LoginController.php

$this->container->setParameter('env(DB_NAME)', 'company');
$env = $this->container->getParameter('env(DB_NAME)');
var_dump($env);
die();

config.yml

companies:
            driver:   pdo_mysql
            host:     '%database_host%'
            port:     '%database_port%'
            dbname:   "%env(DB_NAME)%"
            user:     '%database_user%'
            password: '%database_password%'
            charset:  UTF8

parameters.yml

parameters:
    database_host: 127.0.0.1
    database_port: null
    env(DB_NAME): default # Default value for env
    database_name: "%env(DB_NAME)%"
    database_user: root
    database_password: root

Any idea how to assign the variable?

Inserting the entity manager of all databases is not a solution for me, because there are hundreds of databases where the credentials user / password and table structure is the same. Only change the name of the database.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Well, It's the same exception but I use the environment vars. Thnx! – Dani Marti Oct 12 '17 at 12:50
  • Same answer though. No it is not possible. After configuring the container is actually written out in the cached directory and "frozen". Wherever a parameter has been injected into a service, the actual value of the parameter is written out so the code is not constantly calling getParameter. Thus you can't change parameter values at runtime. It's worth taking a look at the generated container code. – Cerad Oct 12 '17 at 13:04

0 Answers0