Dotenv Usage

From OpenEMR Project Wiki
Revision as of 03:32, 20 May 2017 by Robert Down (talk | contribs) (Created page with "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 d...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

use Dotenv\Dotenv;
$dotenv = new Dotenv(__DIR__); // Will search current directory for a file called .env
$dotenv->load();

// From above example
if (getenv("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