Skip to content

Add url and query_string processors #11128

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
Mar 25, 2019
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
122 changes: 122 additions & 0 deletions configuration/external_parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,128 @@ Symfony provides the following env var processors:

The ``default`` processor was introduced in Symfony 4.3.

``env(url:FOO)``
Parses an absolute URL and returns its components.

.. code-block:: bash

# .env
MONGODB_URL="mongodb://db_user:[email protected]:27017/db_name"

.. configuration-block::

.. code-block:: yaml

# config/packages/mongodb.yaml
mongo_db_bundle:
clients:
default:
hosts:
- { host: '%env(key:host:url:MONGODB_URL)%', port: '%env(key:port:url:MONGODB_URL)%' }
username: '%env(key:user:url:MONGODB_URL)%'
password: '%env(key:pass:url:MONGODB_URL)%'
connections:
default:
database_name: '%env(key:path:url:MONGODB_URL)%'

.. code-block:: xml

<!-- config/packages/mongodb.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"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<mongodb:config>
<mongodb:client name="default" username="%env(key:user:url:MONGODB_URL)%" password="%env(key:pass:url:MONGODB_URL)%">
<mongodb:host host="%env(key:host:url:MONGODB_URL)%" port="%env(key:port:url:MONGODB_URL)%"/>
</mongodb:client>
<mongodb:connections name="default" database_name="%env(key:path:url:MONGODB_URL)%"/>
</mongodb:config>
</container>

.. code-block:: php

// config/packages/mongodb.php
$container->loadFromExtension('mongodb', [
'clients' => [
'default' => [
'hosts' => [
[
'host' => '%env(key:host:url:MONGODB_URL)%',
'port' => '%env(key:port:url:MONGODB_URL)%',
],
],
'username' => '%env(key:user:url:MONGODB_URL)%',
'password' => '%env(key:pass:url:MONGODB_URL)%',
],
],
'connections' => [
'default' => [
'database_name' => '%env(key:path:url:MONGODB_URL)%',
],
],
]);

.. caution::

In order to ease extraction of the resource from the URL, The leading
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The -> the

``/`` is trimed from the ``path`` component.

.. versionadded:: 4.3

The ``url`` processor was introduced in Symfony 4.3.

``env(query_string:FOO)``
Parses an encoded string as if it were the query string passed via a URL.

.. code-block:: bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiereguiluz is bash ok or better terminal or text here for the code block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is the same code block used above in that file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info, maybe we should change them all. Not sure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For information purposes: we decided a few days ago to use bash for "bash things" (such as displaying the content of .env files) and use terminal for real consoles (when we show commands and their outputs).


# .env
MONGODB_URL="mongodb://db_user:[email protected]:27017/db_name?timeout=3000"

.. configuration-block::

.. code-block:: yaml

# config/packages/mongodb.yaml
mongo_db_bundle:
clients:
default:
# ...
connectTimeoutMS: '%env(int:key:timeout:query_string:MONGODB_URL)%'

.. code-block:: xml

<!-- config/packages/mongodb.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"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<mongodb:config>
<mongodb:client name="default" connectTimeoutMS="%env(int:key:timeout:query_string:MONGODB_URL)%" />
</mongodb:config>
</container>

.. code-block:: php

// config/packages/mongodb.php
$container->loadFromExtension('mongodb', [
'clients' => [
'default' => [
// ...
'connectTimeoutMS' => '%env(int:key:timeout:query_string:MONGODB_URL)%',
],
],
]);

.. versionadded:: 4.3

The ``query_string`` processor was introduced in Symfony 4.3.

It is also possible to combine any number of processors:

.. code-block:: yaml
Expand Down