Framework: Can not set env variable during runtime

Created on 8 Dec 2019  路  5Comments  路  Source: laravel/framework

  • Laravel Version: 6.2
  • PHP Version: 7.2
  • Database Driver & Version: pgsql

Description:

when I try to change the env variable in the code during runtime it does not work and gives me the old variable.
I tried
\config(['rabbitmq.worker' => 'default']);
and also
Config::set('rabbitmq_worker','default');
then when I call env('RABBITMQ_WORKER') it gives me null

Most helpful comment

Hello,

Just to clarify, when you set a value with Config facade or config function the changes are valid only in the same request, I noticed you are trying to use RabbitMQ, right? This service runs in a separate process, am I right? You should have in mind when you change something with config() or Config::set() it changes only and exclusively in the same request/process/run time execution, it doesn't change if you try to start a new process nor if in an existent running process,
For example: you set a config key value and try to get its new value from a queue process or new request, it is not gonna work.

All 5 comments

Hello Johnabil, I might be wrong but I guess you are messing something up. When you set something with Config facade or config function you are changing the value that comes from the config file, and you are trying to get the value in the last sentence from .env file or system variables, they are two different things.

Your config file uses the .env file to get some variable from the .env file or from the system variables. But it doesn't mean it is gonna work the other way around.
If you want to get the value from config that you have set before, you should use config('key') or Config::get('key').
Remember config file uses the .env file or system variable to set some of its values.

I tried it with config('rabbitmq.worker') in the queue.php in config folder but it didn't work too

Your description is unclear what doesn't work.

But the point is:

  • changing environment variables during runtime will not reflect in the configuration file, this is expected / not supported
  • changing the config at runtime is fine

Hello,

Just to clarify, when you set a value with Config facade or config function the changes are valid only in the same request, I noticed you are trying to use RabbitMQ, right? This service runs in a separate process, am I right? You should have in mind when you change something with config() or Config::set() it changes only and exclusively in the same request/process/run time execution, it doesn't change if you try to start a new process nor if in an existent running process,
For example: you set a config key value and try to get its new value from a queue process or new request, it is not gonna work.

@mfn @juliano-barros Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

ghost picture ghost  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

PhiloNL picture PhiloNL  路  3Comments