Download the PHP package laxity7/dotenv without Composer
On this page you can find all versions of the php package laxity7/dotenv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download laxity7/dotenv
More information about laxity7/dotenv
Files in laxity7/dotenv
Package dotenv
Short Description Registers environment variables from a .env file
License MIT
Homepage https://github.com/laxity7/dotenv
Informations about the package dotenv
Simply PHP dotenv
Simply Dotenv parser .env files to make environment variables stored in them accessible via getenv(), $_ENV and Env::get().
The class allows working with local variables from .env, as with global environment variables
Install
Install via composer
How to use
The .env file is generally kept out of version control since it can contain sensitive API keys and passwords. A separate .env.example file is created with all the required environment variables defined except for the sensitive ones, which are either user-supplied for their own development environments or are communicated elsewhere to project collaborators. The project collaborators then independently copy the .env.example file to a local .env and ensure all the settings are correct for their local environment, filling in the secret keys or providing their own values when necessary. In this usage, the .env file should be added to the project's .gitignore file so that it will never be committed by collaborators. This usage ensures that no sensitive passwords or API keys will ever be in the version control history so there is less risk of a security breach, and production values will never have to be shared with all project collaborators.
Add your application configuration to a .env file in the root of your project or other place.
Make sure the .env file is added to your .gitignore so it is not checked-in the code.
See all possible use cases in the file "tests/test.env".
Ok, after creating the .env file, you can then load .env in your application.
Basic usage:
There will be no error if the file .env does not exist, only global variables will be taken
Now all the defined variables are available in the $_ENV and $_SERVER super-globals.
If your $_ENV array is mysteriously empty, but you still see the variables when calling getenv() or in your phpinfo(), check your http://us.php.net/manual/en/ini.core.php#ini.variables-order ini setting to ensure it includes "E" in the string.
To get values through the getenv() function use the usePutEnv parameter (but not recommended).
You can also use the get method, which can return a default value (default null
) if a key is missing.
For simplicity, you can create an env helper function
or use helper class Env
or combine both methods
How is this better than symfony/dotenv package?
Firstly, this package is natively support boolean values.
For example,
Secondly, this package is significantly faster than symfony/dotenv, with tests showing it to be up to ~4 times quicker in certain scenarios. This can be a major advantage for projects where performance is critical and every millisecond count.
# | Laxity7 | Symfony |
---|---|---|
min | 0.0218 ms | 0.0773 ms |
max | 0.3831 ms | 0.9657 ms |
avg | 0.0238 ms | 0.0967 ms |
You can run the tests yourself by running the following command:
Thirdly, this package has built-in error smoothing, which allows you to skip common formatting errors (for example, spaces, lack of an equal sign and others) found in .env files. This makes it more user-friendly than symfony/dotenv, which requires you to write the .env file exclusively correctly.
For example, this .env file will be loaded without errors in laxity7/dotenv
But Symfony/dotenv will throw the following exceptions:
Fourth, this package provides inline comments in .env iles.
In summary, this package is a faster and more user-friendly option for managing environment variables on your project compared to symfony/dotenv. While it may not have the same level of community support or features as its competitor, its focus on performance and ease of use may make it an attractive choice for many developers.