Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
Add docs for the Dotenv component#7350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| .. index:: | ||
| single: Dotenv | ||
| single: Components; Dotenv | ||
| The Dotenv Component | ||
| ==================== | ||
| The Dotenv Component parses ``.env`` files to make environment variables | ||
| stored in them accessible via ``getenv()``, ``$_ENV`` or ``$_SERVER``. | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Shouldn't you add a MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. done | ||
| .. versionadded:: 3.3 | ||
| The Dotenv component was introduced in Symfony 3.3. | ||
| Installation | ||
| ------------ | ||
| You can install the component in 2 different ways: | ||
| * :doc:`Install it via Composer </components/using_components>` (``symfony/dotenv`` on `Packagist`_); | ||
| * Use the official Git repository (https://github.com/symfony/dotenv). | ||
| .. include:: /components/require_autoload.rst.inc | ||
| Usage | ||
| ----- | ||
| Sensitive information and environment-dependent settings should be defined as | ||
| environment variables (as recommended for `twelve-factor applications`_). Using | ||
| a ``.env`` file to store those environment variables eases development and CI | ||
| management by keeping them in one "standard" place and agnostic of the | ||
| technology stack you are using (Nginx vs PHP built-in server for instance). | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think we use Nginx everywhere else. MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. indeed, Nginx is used elsewhere | ||
| .. note:: | ||
| PHP has a lot of different implementations of this "pattern". This | ||
| implementation's goal is to replicate what ``source .env`` would do. It | ||
| tries to be as similar as possible with the standard shell's behavior (so | ||
| no value validation for instance). | ||
| Load a ``.env`` file in your PHP application via ``Dotenv::load()``:: | ||
| use Symfony\Component\Dotenv\Dotenv; | ||
| $dotenv = new Dotenv(); | ||
| $dotenv->load(__DIR__.'/.env'); | ||
| // You can also load several files | ||
| $dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev'); | ||
| Given the following ``.env`` file content: | ||
| .. code-block:: bash | ||
| # .env | ||
| DB_USER=root | ||
| DB_PASS=pass | ||
| Access the value with ``getenv()`` in your code:: | ||
| $dbUser = getenv('DB_USER'); | ||
| // you can also use ``$_ENV`` or ``$_SERVER`` | ||
| .. note:: | ||
| Symfony Dotenv never overwrites existing environment variables. | ||
| You should never store a ``.env`` file in your code repository as it might | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. store an MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. No, it's store a dot env file :) Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Right, thanks! | ||
| contain sensitive information; create a ``.env.dist`` file with sensible | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. same here | ||
| defaults instead. | ||
| Symfony Dotenv should only be used in development/testing/staging environments. | ||
| For production environments, use "real" environment variables. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What doSymfony Env stands for? Does it refer to this component? Does it mean we should not use this component in production environment? MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Fixed, should have been Indeed, in production, you should define "real" env vars. | ||
| As a ``.env`` file is a regular shell script, you can ``source`` it in your own | ||
| shell scripts: | ||
| .. code-block:: terminal | ||
| source .env | ||
| Add comments by prefixing them with ``#``: | ||
| .. code-block:: bash | ||
| # Database credentials | ||
| DB_USER=root | ||
| DB_PASS=pass # This is the secret password | ||
| Use environment variables in values by prefixing variables with ``$``: | ||
| .. code-block:: bash | ||
| DB_USER=root | ||
| DB_PASS=${DB_USER}pass # Include the user as a password prefix | ||
| Embed commands via ``$()`` (not supported on Windows): | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Windows 10 added support for a Bash shell, so is this is still valid? Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Windows is not able to do it out of the box. But you can use it through an emulator like cmder or ConEmu. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Wait ConEmu support this? Cool :) Windows 10 added bash support, but using an Ubuntu subsystem 😵 so it will work as expected then. | ||
| .. code-block:: bash | ||
| START_TIME=$(date) | ||
| .. note:: | ||
| Note that using ``$()`` might not work depending on your shell. | ||
| .. _Packagist: https://packagist.org/packages/symfony/dotenv | ||
| .. _twelve-factor applications: http://www.12factor.net/ | ||