Skip to content

[DependencyInjection] Add urlencode function to EnvVarProcessor #19150

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
Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions configuration/env_var_processors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,56 @@ Symfony provides the following env var processors:
// config/services.php
$container->setParameter('typed_env', '%env(defined:FOO)%');

.. _urlencode_environment_variable_processor:

``env(urlencode:FOO)``
Urlencode the content of ``FOO`` env var. This is especially useful when
``FOO`` value is not compatible with DSN syntax.

.. configuration-block::

.. code-block:: yaml

# config/packages/framework.yaml
parameters:
env(DATABASE_URL): 'mysql://db_user:foo@[email protected]:3306/db_name'
encoded_database_url: '%env(urlencode:DATABASE_URL)%'

.. code-block:: xml

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<parameters>
<parameter key="env(DATABASE_URL)">mysql://db_user:foo@[email protected]:3306/db_name</parameter>
<parameter key="encoded_database_url">%env(urlencode:DATABASE_URL)%</parameter>
</parameters>
</container>

.. code-block:: php

// config/packages/framework.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Config\FrameworkConfig;

return static function (ContainerBuilder $container): void {
$container->setParameter('env(DATABASE_URL)', 'mysql://db_user:foo@[email protected]:3306/db_name');
$container->setParameter('encoded_database_url', '%env(urlencode:DATABASE_URL)%');
};

.. versionadded:: 7.1

The ``env(urlencode:...)`` env var processor was introduced in Symfony 7.1.

It is also possible to combine any number of processors:

.. configuration-block::
Expand Down
9 changes: 5 additions & 4 deletions doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ The database connection information is stored as an environment variable called

If the username, password, host or database name contain any character considered
special in a URI (such as ``+``, ``@``, ``$``, ``#``, ``/``, ``:``, ``*``, ``!``, ``%``),
you must encode them. See `RFC 3986`_ for the full list of reserved characters or
use the :phpfunction:`urlencode` function to encode them. In this case you need to
remove the ``resolve:`` prefix in ``config/packages/doctrine.yaml`` to avoid errors:
``url: '%env(DATABASE_URL)%'``
you must encode them. See `RFC 3986`_ for the full list of reserved characters.
You can use the :phpfunction:`urlencode` function to encode them or
the :ref:`urlencode environment variable processor <urlencode_environment_variable_processor>`.
In this case you need to remove the ``resolve:`` prefix in ``config/packages/doctrine.yaml``
to avoid errors: ``url: '%env(DATABASE_URL)%'``

Now that your connection parameters are setup, Doctrine can create the ``db_name``
database for you:
Expand Down