![]() |
Reload .env file in runtime - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Reload .env file in runtime (/showthread.php?tid=91518) |
Reload .env file in runtime - dhiya as - 08-24-2024 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.. RE: Reload .env file in runtime - kenjis - 08-24-2024 Try PHP Code: (new DotEnv($paths->appDirectory . '/../'))->load(); RE: Reload .env file in runtime - dhiya as - 08-25-2024 (08-24-2024, 07:45 PM)kenjis Wrote: Tryhi 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 ![]() ![]() the .env has generate n load.but the config not change ![]() RE: Reload .env file in runtime - kenjis - 08-25-2024 (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. RE: Reload .env file in runtime - kenjis - 08-25-2024 The solution is not to call config(\Config\Database::class) before reloading dot env file. RE: Reload .env file in runtime - dhiya as - 08-25-2024 (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... |