Welcome Guest, Not a member yet? Register   Sign In
Reload .env file in runtime
#1

hi..
is it possible to reload the .env in runtime?
or is there a function I can use to reload the .env?

i have create new command in spark to installation my project,
with extend class BaseCommand.

i create function to generate  .env file with the value from input CLI .
before generate i validation that, include checking connection DB,table exist..etc  n i use value from CLI.

when the input valid  then i generate the .env file with that value.


the problem is  when i generate the .env in runtime, 
its cant change the first load value,  before i generate that.

a new .env can be read after the second load.
i must stop the CLI n run command again..

i want to automaticly read the env in global after i generate that.
so i can run the others command, like migration,seed automaticly after i generate the env...

can i do that?

thanks before..
Reply
#2

Try
PHP Code:
(new DotEnv($paths->appDirectory '/../'))->load(); 
Reply
#3
Photo 

(08-24-2024, 07:45 PM)kenjis Wrote: Try
PHP Code:
(new DotEnv($paths->appDirectory '/../'))->load(); 
hi kenjis..i have try that..but not work..
DotEnv successfuly load the new .env, but the configuration DB still use default value,not use value from the new .env

step
1. first i dont have .env,
So i Running in first time the CLI ,and i Check the config DB in this case CI automatic load default config DB.

2. i run my command to automaticly generate .env , load that with DotEnv ,
and check again with config:check Database, but the value config DB not change.

Code:
        //step  Checking first time config db
        CLI::wait(2);
        CLI::write('Checking first time config db`...', 'black', 'green');
        command('config:check Database');

        //step 2 Generate env
        CLI::wait(2);
        CLI::write('Generate env `key:generate`...', 'black', 'green');
        $this->call('key:generate');

        //passing value in .env
        CLI::wait(2);
        CLI::write('Running `passing value env`...', 'black', 'green');
        $this->setEnv($domain, $dbHost, $dbName, $dbUser, $dbPassword, $driver, $port);
        CLI::showProgress($currStep++, $totalSteps);

        //step 3 clear cache
        CLI::wait(2);
        CLI::write('Running `Clear Cache`...', 'black', 'green');
        command('cache:clear');

        //step 4 load New ENV
        CLI::wait(2);
        CLI::write('Load `Env`...', 'black', 'green');
        $paths = new Paths();
        (new DotEnv($paths->appDirectory . '/../'))->load();


        //step 5 Checking if config DB has change with new ENV
        CLI::wait(2);
        CLI::write('Checking if config DB has change with new ENV', 'black', 'green');
        command('config:check Database');
        die; //stop just testing


[Image: ad.jpg]

[Image: ad.jpg]

the .env has generate n load.but the config not change
[Image: ad.jpg]
Reply
#4

(08-25-2024, 12:07 AM)dhiya as Wrote: DotEnv successfuly load the new .env, but the configuration DB still use default value,not use value from the new .env

Yes. The Config object is set the properties when it is instantiated.
If you instantiate the Config object before reloading dot env file, the Config object still has the values before creating dot env file.
Reply
#5

The solution is not to call config(\Config\Database::class) before reloading dot env file.
Reply
#6
Star 

(08-25-2024, 02:21 AM)kenjis Wrote: The solution is not to call config(\Config\Database::class) before reloading dot env file.

Wow..Great.. thanks Kenji .. its work..

the command  ('config:check Database') also call config(\Config\Database::class)


i remove that and Done...

Thanks...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB