Skip to content

Commit 14f8d58

Browse files
committed
added docs for the env component
1 parent e41f358 commit 14f8d58

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

components/dotenv.rst

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
.. index::
2+
single: Dotenv
3+
single: Components; Dotenv
4+
5+
The Dotenv Component
6+
====================
7+
8+
The Dotenv Component parses ``.env`` files to make environment variables
9+
stored in them accessible via ``getenv()``, ``$_ENV`` or ``$_SERVER``.
10+
11+
Installation
12+
------------
13+
14+
You can install the component in 2 different ways:
15+
16+
* :doc:`Install it via Composer </components/using_components>` (``symfony/dotenv`` on `Packagist`_);
17+
* Use the official Git repository (https://github.com/symfony/dotenv).
18+
19+
.. include:: /components/require_autoload.rst.inc
20+
21+
Usage
22+
-----
23+
24+
Sensitive information and environment-dependent settings should be defined as
25+
environment variables (as recommended for `twelve-factor applications`_. Using
26+
a ``.env`` file to store those environment variables eases development and CI
27+
management by keeping them in one "standard" place and agnostic of the
28+
technology stack you are using (Nginx vs PHP built-in server for instance).
29+
30+
.. note::
31+
32+
PHP has a lot of different implementations of this "pattern". This
33+
implementation's goal is to replicate what ``source .env`` would do. It
34+
tries to be as similar as possible with the standard shell's behavior (so
35+
no value validation for instance).
36+
37+
Load a ``.env`` file in your PHP application via ``Dotenv::load()``::
38+
39+
use Symfony\Component\Dotenv\Dotenv;
40+
41+
$dotenv = new Dotenv();
42+
$dotenv->load(__DIR__.'/.env');
43+
44+
// You can also load several files
45+
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev');
46+
47+
Given the following ``.env`` file content:
48+
49+
.. code-block:: bash
50+
51+
# .env
52+
DB_USER=root
53+
DB_PASS=pass
54+
55+
Access the value with ``getenv()`` in your code::
56+
57+
$dbUser = getenv('DB_USER');
58+
// you can also use ``$_ENV`` or ``$_SERVER``
59+
60+
.. note::
61+
62+
Symfony Env never overwrites existing environment variables.
63+
64+
You should never store a ``.env`` file in your code repository as it might
65+
contain sensitive information; create a ``.env.dist`` file with sensible
66+
defaults instead.
67+
68+
Symfony Env should only be used in development/testing/staging environments.
69+
For production environments, use "real" environment variables.
70+
71+
As a ``.env`` file is a regular shell script, you can ``source`` it in your own
72+
shell scripts:
73+
74+
.. code-block:: terminal
75+
76+
source .env
77+
78+
Add comments by prefixing them with ``#``:
79+
80+
.. code-block:: bash
81+
82+
# Database credentials
83+
DB_USER=root
84+
DB_PASS=pass # This is the secret password
85+
86+
Use environment variables in values by prefixing variables with ``$``:
87+
88+
.. code-block:: bash
89+
90+
DB_USER=root
91+
DB_PASS=${DB_USER}pass # Include the user as a password prefix
92+
93+
Embed commands via ``$()`` (not supported on Windows):
94+
95+
.. code-block:: bash
96+
97+
START_TIME=$(date)
98+
99+
.. note::
100+
101+
Note that using ``$()`` might not work depending on your shell.
102+
103+
.. _Packagist: https://packagist.org/packages/symfony/dotenv
104+
.. _twelve-factor applications: http://www.12factor.net/

0 commit comments

Comments
 (0)