Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbf2911f

Browse files
committed
added docs for the env component
1 parente41f358 commitbf2911f

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

‎components/env.rst‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 build-in
29+
server for instance).
30+
31+
..note::
32+
33+
PHP has a lot of different implementation of this "pattern". This
34+
implementation goal is to replicate what ``source .env`` would do. So, it
35+
tries to be as similar as possible with the default shells 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::
44+
45+
composer require symfony/env
46+
47+
Load an ``.env`` file in your PHP application via ``Dotenv::load()``:
48+
49+
..code-block::php
50+
51+
use Symfony\Component\Env\Dotenv;
52+
53+
$dotenv = new Dotenv();
54+
$dotenv->load(__DIR__.'/.env');
55+
56+
// You can also load several files
57+
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev');
58+
59+
Given the following ``.env`` file content:
60+
61+
..code-block::bash
62+
63+
DB_USER=root
64+
DB_PASS=pass
65+
66+
Access the value with ``getenv()`` in your code:
67+
68+
..code-block::php
69+
70+
$dbUser = getenv('DB_USER);
71+
// you can also use ``$_ENV`` or ``$_SERVER``
72+
73+
..note::
74+
75+
Symfony Env never overwrites existing environment variables.
76+
77+
You should never store a ``.env`` file is your code repository as it might
78+
contains sensitive information; creates a ``.env.dist`` file with sensible
79+
defaults instead.
80+
81+
Symfony Env should only be used in development/testing/staging environments.
82+
For production environments, use "real" environment variables.
83+
84+
As a ``.env`` file is a regular shell script, you can ``source`` it in your own
85+
shell scripts:
86+
87+
..code-block::bash
88+
89+
source .env
90+
91+
Add comments by prefixing them with ``#``:
92+
93+
..code-block::bash
94+
95+
# Database credentials
96+
DB_USER=root
97+
DB_PASS=pass# This is the secret password
98+
99+
Use environment variables in values by prefixing variables with ``$``:
100+
101+
..code-block::bash
102+
103+
DB_USER=root
104+
DB_PASS=${DB_USER}pass# Include the user as a password prefix
105+
106+
Embed commands via ``$()``:
107+
108+
..code-block::bash
109+
110+
START_TIME=$(date)
111+
112+
..note::
113+
114+
Note that using ``$()`` might not work depending on your shell.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp