Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[DependencyInjection] Document %env(...)% dynamic parameters#6918
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.
Conversation
| $container->setParameter('env(DATABASE_HOST)', 'localhost'); | ||
| Unlike ``SYMFONY__`` prefixed environment variables, ``%env(...)%`` parameters are | ||
| truned into run-time environment variable reads, so that dumped containers can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
turned
d816802 to9f7017eCompare| be reconfigured dynamically even after being compiled. | ||
| ..versionadded::3.2 | ||
| ``env(...)`` parameters were introduced in Symfony 3.2. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
please put the versionadded directive after the section header, instead of at the end
wouterj commentedAug 28, 2016
status: needs work |
…env(MY_ENV_VAR)% (nicolas-grekas)This PR was merged into the 3.2-dev branch.Discussion----------[DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)%| Q | A| ------------- | ---| Branch? | master| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#10138,#7555,#16403,#18155| License | MIT| Doc PR |symfony/symfony-docs#6918This is an alternative approach to#18155 for injecting env vars into container configurations.With this PR, anywhere parameters are allowed, one can use `%env(ENV_VAR)%` to inject a dynamic env var. Additionally, if one sets a value to such parameters in e.g. the `parameter.yml` file (`env(ENV_VAR): foo`), this value will be used as a default value when the env var is not defined. If no default value is specified, an `EnvVarNotFoundException` will be thrown at runtime.Unlike previous attempts that also used parameters (#16403), the implementation is compatible with DI extensions: before being dumped, env vars are resolved to uniquely identifiable string placeholders that can get through DI extensions manipulations. When dumped, these unique placeholders are replaced by dynamic calls to a getEnv method..ping@magnusnordlander@dzuelke@fabpotCommits-------bac2132 [DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)% syntax
…env(MY_ENV_VAR)% (nicolas-grekas)This PR was merged into the 3.2-dev branch.Discussion----------[DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)%| Q | A| ------------- | ---| Branch? | master| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | #10138, #7555, #16403, #18155| License | MIT| Doc PR |symfony/symfony-docs#6918This is an alternative approach to #18155 for injecting env vars into container configurations.With this PR, anywhere parameters are allowed, one can use `%env(ENV_VAR)%` to inject a dynamic env var. Additionally, if one sets a value to such parameters in e.g. the `parameter.yml` file (`env(ENV_VAR): foo`), this value will be used as a default value when the env var is not defined. If no default value is specified, an `EnvVarNotFoundException` will be thrown at runtime.Unlike previous attempts that also used parameters (#16403), the implementation is compatible with DI extensions: before being dumped, env vars are resolved to uniquely identifiable string placeholders that can get through DI extensions manipulations. When dumped, these unique placeholders are replaced by dynamic calls to a getEnv method..ping@magnusnordlander@dzuelke@fabpotCommits-------bac2132 [DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)% syntax
…env(MY_ENV_VAR)% (nicolas-grekas)This PR was merged into the 3.2-dev branch.Discussion----------[DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)%| Q | A| ------------- | ---| Branch? | master| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | #10138, #7555, #16403, #18155| License | MIT| Doc PR |symfony/symfony-docs#6918This is an alternative approach to #18155 for injecting env vars into container configurations.With this PR, anywhere parameters are allowed, one can use `%env(ENV_VAR)%` to inject a dynamic env var. Additionally, if one sets a value to such parameters in e.g. the `parameter.yml` file (`env(ENV_VAR): foo`), this value will be used as a default value when the env var is not defined. If no default value is specified, an `EnvVarNotFoundException` will be thrown at runtime.Unlike previous attempts that also used parameters (#16403), the implementation is compatible with DI extensions: before being dumped, env vars are resolved to uniquely identifiable string placeholders that can get through DI extensions manipulations. When dumped, these unique placeholders are replaced by dynamic calls to a getEnv method..ping@magnusnordlander@dzuelke@fabpotCommits-------bac2132 [DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)% syntax
weaverryan left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Status: Reviewed
Setting this as reviewed because all of my comments are very minor!
| ..code-block::yaml | ||
| parameters: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Add#app/config/parameters.yml above this - it'sprobably the file where they will define the default value
| )); | ||
| ``env(...)`` parameters can have default values be set and used whenever their | ||
| corresponding environment variables are not found: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You can also give the ``env(...)`` parameters a default value: the default value will be used wheneverthe corresponding environment variable is *not* found:
| ..note:: | ||
| All popular web servers support setting environment variables. Read their | ||
| documentation to know how. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
... to find out how.
| ..note:: | ||
| You can also define the default value of any existing parameters using | ||
| special environement variables named after their corresponding parameter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
environment
weaverryan commentedSep 18, 2016
Status: Reviewed Just needs a few minor changes. |
| You can also define the default value of any existing parameters using | ||
| special environement variables named after their corresponding parameter | ||
| prefixed with ``SYMFONY__`` after replacing dots by double underscores | ||
| (e.g. ``SYMFONY__FOO__BAR`` to set the default value of the ``foo.bar`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
let's keep using a real example here:(e.g.SYMFONY__DATABASE__USERto set the default value of thedatabase.user` parameter).
3021c4f to68e9b49Comparenicolas-grekas commentedSep 19, 2016
Comments addressed, thanks for the review. |
wouterj commentedOct 1, 2016
👍 |
| #app/config/parameters.yml | ||
| parameters: | ||
| env(DATABASE_HOST):localhost |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Is this functionality part of thesymfony/symfony#19681 PR? I've tried something similar with:
database_driver: 'env(DATABASE_DRIVER): pdo_mysql'
but it doesn't use the default and just complains that:
"The given 'driver' env(DATABASE_DRIVER): pdo_mysql is unknown"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@mickadoo your code isnot what is documented here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@stof I just thought I'd try it out, didn't work though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
What's documented here is:
parameters:database_driver:'%env(DATABASE_DRIVER)%'env(DATABASE_DRIVER):pdo_mysql
Which, as you can see, is quite different from the format you tested. If there is something we can do to improve the description of the feature, please tell us!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@wouterj Ah, I get it now. Sorry about that!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
No problem@mickadoo thanks for test-casing this documentation proposal.
I think we should include thedatabase_driver: '%env(DATABASE_DRIVER)%' line in this example to avoid confusing for other readers.
| You can now reference these parameters wherever you need them. | ||
| For example, if you want to use the value of the ``DATABASE_HOST`` environment | ||
| variable into you service container configuration, you can reference it using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
[...] variable in your service container configuration [...]
| ..code-block::yaml | ||
| #app/config/parameters.yml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
missing space between comment and "app"
| ..code-block::yaml | ||
| #app/config/parameters.yml | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
blank line should be removed
| ..code-block::xml | ||
| <?xml version="1.0" encoding="UTF-8" ?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
missing filename comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
same for the PHP config example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Do you have an example of such filenames for xml & php?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We usually simply replace the file extension. So here we would haveapp/config/parameters.yml,app/config/parameters.xml, andapp/config/parameters.php. By the way, can you also add similar comments in the example above to, but usingconfig.yml,config.xml, andconfig.php there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Ah well, the previous example is not new. We should add the comments there in older branches too and can probably better do that in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
got it, fixed
| $container->setParameter('env(DATABASE_HOST)', 'localhost'); | ||
| If you're using Apache, environment variables can be set e.g. using the following |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Everywhere else we give examples for Nginx too IIRC. I think we should do the same here.
| <Directory "/path/to/symfony_2_app/web"> | ||
| AllowOverride All | ||
| Allow from All | ||
| </Directory> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We should also add a placeholder comment to indicate that this config is not a complete working examples, but only shows how to configure environment variables for a virtual host.
wouterj commentedOct 16, 2016
status: needs work ping@nicolas-grekas |
nicolas-grekas commentedOct 21, 2016
comments addressed |
| ..code-block::terminal | ||
| You can reference environment variables by using special parameters named after | ||
| the variables you want to use enclosed between ``env()``. Their actual values | ||
| will be resolved at runtime, so that dumped containers can be reconfigured |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
A quick question about this:
Their actual values will be resolved at runtime [...]Are they resolved at runtimeevery time they are used ... or are they resolvedjust the first time they are used and then Symfony reuses the cached value? I say this because ofthe code of the getEnv() method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Per request they are resolved once. But if you change the value, during the next request you will see the new value (again resolved only once during this request). Should we make that more clear somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks@xabbuh. I wouldn't add any notice about this because that behavior is exactly what we should expect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I added(once per request), good enough?
| ..configuration-block:: | ||
| ..code-block::apache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
looking at the tests,apache here makes sphinx fail, any clue why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Apache has no label configured. Please edit the_build/conf.py file and add:'apache': 'Apache' (same for the other formats in this block). (and also ping javier that the symfony.com config has to be updated)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@wouterj thanks for the heads up. It will be merged in symfony.com soon:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@javiereguiluz please note that you also have to add'nginx': 'NGINX'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@wouterj thanks! It's fixed now. I opted forNginx as the human-readable name instead ofNGINX. The official website mixesnginx,Nginx andNGINX ... but the Wikipedia always usesNginx.
wouterj commentedNov 9, 2016
👍 Note to merger: Please fix the |
wouterj commentedNov 19, 2016
ping@javiereguiluz@weaverryan@xabbuh please review & vote |
| ) | ||
| )); | ||
| You can also give the ``env()`` parameters a default value: the default value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
... a default value: the default value will be used whenever ...
-->
... a default value used whenever ...
javiereguiluz left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
👍
xabbuh commentedDec 8, 2016
👍 |
xabbuh commentedDec 14, 2016
Thank you Nicolas. |
…eters (nicolas-grekas)This PR was merged into the 3.2 branch.Discussion----------[DependencyInjection] Document %env(...)% dynamic parametersRef.symfony/symfony#19681Commits-------083da46 [DI] Document %env()% dynamic parameters

Uh oh!
There was an error while loading.Please reload this page.
Ref.symfony/symfony#19681