score:1

Accepted answer

steps:

  1. open the file index.php under the codeigniter 4 application's public path /root/ci4/public.

  2. load the .env file of the react application just before the line of code $app->run(); in the file /root/ci4/public/index.php:

// ...

// load environment settings from .env file into $_server and $_env
$envpath = rootpath . ".." . directory_separator . "react";
$envfilename = ".env";

if (is_file($file = $envpath . directory_separator . $envfilename) && is_readable($file)) (new \codeigniter\config\dotenv($envpath, $envfilename))->load();

// ...

where:

  • $envpath - represents the directory path of the "react application's" .env file. it's assumed that it resides under /root/react/.env

  • $envfilename - represents the "react application's" .env file name.

  • \codeigniter\config\dotenv(...)->load() - this method is the one responsible for loading the .env file and processing it so that we end up with all settings in the php environment vars (i.e. getenv(), $_env, and $_server).

extra notes:

a.

if a .env variable already exists in the environment, it will not be overwritten. - environment variables and codeigniter

this means that an environment variable defined under /root/react/.env won't overwrite a similar one defined under /root/ci4/.env.

b.

warning: note that your settings from the .env file are added to environment variables. as a side effect, this means that if your codeigniter application is (for example) generating a var_dump($_env) or phpinfo() (for debugging or other valid reasons) your secure credentials are publicly exposed. - environment variables and codeigniter


Related Query

More Query from same tag