Difference between revisions of "Dotenv Usage"

From OpenEMR Project Wiki
 
Line 17: Line 17:
use Dotenv\Dotenv;
use Dotenv\Dotenv;
if (file_exists("{$webserver_root}/.env") {
if (file_exists("{$webserver_root}/.env") {
  $dotenv = Dotenv::createImmutable($webserver_root);
    $dotenv = Dotenv::createImmutable($webserver_root);
     $dotenv->load();
     $dotenv->load();
}
}

Latest revision as of 06:35, 8 September 2020

Starting with version 5.0.1 OpenEMR now supports "Dotenv" files to better enable development. A dotenv file is a file, excluded from version control, that allows each developer to set specific variables special to the development environment (Or for production testing). An example would be testing interaction with an online cloud service (such as Amazon AWS) where the dev has an access and secret key. Those keys should not be stored in git, code, or a database, they should instead be set on the server level. By utilizing a .env file, you can simulate that server variable without having to actually go in and mess around under the hood.

In the root directory copy .env.example to .env and set the OPENEMR__ENVIRONMENT variable to dev

Currently this only has one effect: ensure the frontend assets are refreshed on every request (It overrides the $v_js_includes variable with a random integer).


Example .env file

OPENEMR__ENVIRONMENT=dev

Future use could include special logging or profiling during development. However, please be wise on where to use this; if you're writing lots of functions that use this feature, you may need to reconsider how you're writing the functions.


Example usage in code

// should only be used in globals.php, is referenced here just as an example
use Dotenv\Dotenv;
if (file_exists("{$webserver_root}/.env") {
    $dotenv = Dotenv::createImmutable($webserver_root);
    $dotenv->load();
}

// From above example
if ($_ENV["OPENEMR__ENVIRONMENT"] === "dev") {
    // do something specitifc for a dev site
} else {
    // do something else for any other site (Right now just `prod`)
    // Or, if there's nothing special to happen, just omit the else clause
}


To learn more about Dotenv and some more usage, see the github repository