8000 feature #7350 Add docs for the Dotenv component (fabpot) · symfony/symfony-docs@97b9809 · GitHub
[go: up one dir, main page]

Skip to content

Commit 97b9809

Browse files
committed
feature #7350 Add docs for the Dotenv component (fabpot)
This PR was merged into the master branch. Discussion ---------- Add docs for the Dotenv component See symfony/symfony#21234 Commits ------- 03fda49 added docs for the env component
2 parents c45a7f2 + 03fda49 commit 97b9809

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

components/dotenv.rst

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

0 commit comments

Comments
 (0)
0