Skip to content

[Dotenv] Fix incorrect way to modify .env path #19377

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -936,22 +936,45 @@ Storing Environment Variables In Other Files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

By default, the environment variables are stored in the ``.env`` file located
at the root of your project. However, you can store them in other file by
setting the ``SYMFONY_DOTENV_PATH`` environment variable to the absolute path of
that custom file.
at the root of your project. However, you can store them in other files in
multiple ways.

If you use the :doc:`Runtime component </components/runtime>`, the dotenv
path is part of the options you can set in your ``composer.json`` file:

.. code-block:: json

{
// ...
"extra": {
// ...
"runtime": {
"dotenv_path": "my/custom/path/to/.env"
}
}
}

You can also set the ``SYMFONY_DOTENV_PATH`` environment variable at system
level (e.g. in your web server configuration or in your Dockerfile):

.. code-block:: bash

# .env (or .env.local)
SYMFONY_DOTENV_PATH=my/custom/path/to/.env

Finally, you can directly invoke the ``Dotenv`` class in your
``bootstrap.php`` file or any other file of your application::

use Symfony\Component\Dotenv\Dotenv;

(new Dotenv())->bootEnv(dirname(__DIR__).'my/custom/path/to/.env');

Symfony will then look for the environment variables in that file, but also in
the local and environment-specific files (e.g. ``.*.local`` and
``.*.<environment>.local``). Read
:ref:`how to override environment variables <configuration-multiple-env-files>`
to learn more about this.

.. caution::

The ``SYMFONY_DOTENV_PATH`` environment variable must be defined at the
system level (e.g. in your web server configuration) and not in any default
or custom ``.env`` file.

.. versionadded:: 7.1

The ``SYMFONY_DOTENV_PATH`` environment variable was introduced in Symfony 7.1.
Expand Down