Skip to content

Commit 38a171f

Browse files
committed
added docs for the env component
1 parent e41f358 commit 38a171f

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

components/env.rst

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
.. index::
2+
single: Env
3+
single: Components; Env
4+
5+
The Env Component
6+
=================
7+
8+
The Env 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/env`` on `Packagist`_);
17+
* Use the official Git repository (https://github.com/symfony/env).
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
26+
<http://www.12factor.net/>`_. Using an `.env` file to store those environment
27+
variables eases development and CI management by keeping them in one "standard"
28+
place and agnostic of the technology stack you are using (Nginx vs PHP built-in
29+
server for instance).
30+
31+
.. note::
32+
33+
PHP has a lot of different implementations of this "pattern". This
34+
implementation's goal is to replicate what ``source .env`` would do. So, it
35+
tries to be as similar as possible with the default shell's behavior, and
36+
does not do anything not possible via ``source`` (like value validation).
37+
38+
Usage
39+
-----
40+
41+
First, require Symfony Env via Composer:
42+
43+
.. code-block:: bash
44+
45+
composer require symfony/env
46+
47+
Load an ``.env`` file in your PHP application via ``Dotenv::load()``::
48+
49+
use Symfony\Component\Env\Dotenv;
50+
51+
$dotenv = new Dotenv();
52+
$dotenv->load(__DIR__.'/.env');
53+
54+
// You can also load several files
55+
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev');
56+
57+
Given the following ``.env`` file content:
58+
59+
.. code-block:: bash
60+
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 ``$()``:
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.

0 commit comments

Comments
 (0)