vlucas/phpdotenv's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.
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
$_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
$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.