vlucas/phpdotenv
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
graham-campbell/result-type | v1.1.2 | - | MIT | prod | |
phpoption/phpoption | 1.9.2 | - | Apache-2.0 | prod | |
symfony/polyfill-ctype | v1.28.0 | 4.71 kB | MIT | prod | |
symfony/polyfill-mbstring | v1.28.0 | 29.41 kB | MIT | prod | |
symfony/polyfill-php80 | v1.28.0 | 8.01 kB | MIT | prod |
The vlucas/phpdotenv is a PHP package that helps developers manage environmental variables for their applications. This package automagically loads environment variables from a .env
file in your project to getenv()
, $_ENV
and $_SERVER
. This ensures that sensitive credentials, like database passwords or API keys, are never stored directly in your code, providing an enhanced level of security.
Using the vlucas/phpdotenv in your PHP project is straightforward. The first step is to install it via Composer using the command composer require vlucas/phpdotenv
. After the installation, you create a .env
file in the root of your project and add your application's configuration parameters, making sure the .env
file is added to your .gitignore
so it's not checked-in the code.
You can load the variables in your PHP application as follows:
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
This code will load the .env
file from your project root directory. If you want to suppress the exception thrown for a missing .env
file, you can use safeLoad():
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
You can access the loaded variables via the $_ENV
and $_SERVER
super-globals:
$s3_bucket = $_ENV['S3_BUCKET'];
$s3_bucket = $_SERVER['S3_BUCKET'];
The documentation for the vlucas/phpdotenv package is not provided separately, but comprehensive usage instructions are available directly in the readme file on the package's GitHub page. It provides details on installation, upgrading, creating and loading the environment variables, as well as more advanced usage examples including setting immutability, nesting variables, requiring variables to be set, and customizing repositories.