Skip to content

Commit 48789e8

Browse files
committed
added docs for the env component
1 parent e41f358 commit 48789e8

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

components/dotenv.rst

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
an ``.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. So, it
34+
tries to be as similar as possible with the default shell's behavior, and
35+
does not do anything not possible via ``source`` (like value validation).
36+
37+
Usage
38+
-----
39+
40+
First, require Symfony Dotenv via Composer:
41+
42+
.. code-block:: bash
43+
44+
composer require symfony/dotenv
45+
46+
Load an ``.env`` file in your PHP application via ``Dotenv::load()``::
47+
48+
use Symfony\Component\Dotenv\Dotenv;
49+
50+
$dotenv = new Dotenv();
51+
$dotenv->load(__DIR__.'/.env');
52+
53+
// You can also load several files
54+
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev');
55+
56+
Given the following ``.env`` file content:
57+
58+
.. code-block:: bash
59+
60+
# .env
61+
DB_USER=root
62+
DB_PASS=pass
63+
64+
Access the value with ``getenv()`` in your code::
65+
66+
$dbUser = getenv('DB_USER');
67+
// you can also use ``$_ENV`` or ``$_SERVER``
68+
69+
.. note::
70+
71+
Symfony Env never overwrites existing environment variables.
72+
73+
You should never store a ``.env`` file in your code repository as it might
74+
contain sensitive information; create a ``.env.dist`` file with sensible
75+
defaults instead.
76+
77+
Symfony Env should only be used in development/testing/staging environments.
78+
For production environments, use "real" environment variables.
79+
80+
As a ``.env`` file is a regular shell script, you can ``source`` it in your own
81+
shell scripts:
82+
83+
.. code-block:: bash
84+
85+
source .env
86+
87+
Add comments by prefixing them with ``#``:
88+
89+
.. code-block:: bash
90+
91+
# Database credentials
92+
DB_USER=root
93+
DB_PASS=pass # This is the secret password
94+
95+
Use environment variables in values by prefixing variables with ``$``:
96+
97+
.. code-block:: bash
98+
99+
DB_USER=root
100+
DB_PASS=${DB_USER}pass # Include the user as a password prefix
101+
102+
Embed commands via ``$()`` (not supported on Windows):
103+
104+
.. code-block:: bash
105+
106+
START_TIME=$(date)
107+
108+
.. note::
109+
110+
Note that using ``$()`` might not work depending on your shell.
111+
112+
.. _Packagist: https://packagist.org/packages/symfony/dotenv
113+
.. _twelve-factor applications: http://www.12factor.net/

0 commit comments

Comments
 (0)