mirror of
https://github.com/symfony/symfony-docs.git
synced 2026-03-23 16:22:10 +01:00
[Configuration][Doctrine] Document .env quoting and separate connection parameters for special characters
This commit is contained in:
@@ -824,6 +824,21 @@ Define a default value in case the environment variable is not set:
|
||||
DB_USER=
|
||||
DB_PASS=${DB_USER:-root}pass # results in DB_PASS=rootpass
|
||||
|
||||
Wrap values with single quotes to use them as literal strings where ``$``,
|
||||
``#`` and other special characters have no special meaning:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
DB_PASS='p@ss#w$rd'
|
||||
|
||||
With double quotes, variables are still interpolated but ``#`` and other
|
||||
characters are treated as literal:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
DB_PASS="p@ss#word"
|
||||
DB_NAME="my_${DB_USER}_database"
|
||||
|
||||
Embed commands via ``$()`` (not supported on Windows):
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
27
doctrine.rst
27
doctrine.rst
@@ -68,6 +68,33 @@ The database connection information is stored as an environment variable called
|
||||
In this case you need to remove the ``resolve:`` prefix in ``config/packages/doctrine.yaml``
|
||||
to avoid errors: ``url: '%env(DATABASE_URL)%'``
|
||||
|
||||
.. tip::
|
||||
|
||||
To avoid URL-encoding issues with special characters in credentials, you can
|
||||
use separate connection parameters instead of the URL format. Define each
|
||||
value as its own environment variable and wrap it in single quotes in the
|
||||
``.env`` file to prevent characters like ``$`` and ``#`` from being
|
||||
interpreted:
|
||||
|
||||
.. code-block:: text
|
||||
|
||||
# .env
|
||||
DATABASE_PASSWORD='p@ss$wo#rd'
|
||||
|
||||
Then configure Doctrine to use individual parameters:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
# config/packages/doctrine.yaml
|
||||
doctrine:
|
||||
dbal:
|
||||
user: '%env(DATABASE_USER)%'
|
||||
password: '%env(DATABASE_PASSWORD)%'
|
||||
host: '%env(DATABASE_HOST)%'
|
||||
port: '%env(DATABASE_PORT)%'
|
||||
dbname: '%env(DATABASE_NAME)%'
|
||||
driver: pdo_mysql
|
||||
|
||||
Now that your connection parameters are setup, Doctrine can create the ``db_name``
|
||||
database for you:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user