I did a fresh Symfony installation by using Symfony Flex and the new skeleton belong to the next Symfony 4 directory structure.
I add and configure a first third-party bundle : HWIOAuthBundle. This bundle is used to connect via Twitter using two secret information.
I declare my consumer_id
and my consumer_secret
in the config/packages/hwi_oauth.yaml
file.
hwi_oauth:
firewall_names: [secured_area]
resource_owners:
twitter:
type: twitter
client_id: XXXXXMyIdXXXXX
client_secret: XXXXXMyTopSecretKeyXXXXX
My application works fine. But I cannot commit my secrets on github!
I want to have a hwi_oauth.yaml
file like this one:
hwi_oauth:
firewall_names: [secured_area]
resource_owners:
twitter:
type: twitter
client_id: '%twitter_consumer_id%'
client_secret: '%twitter_consumer_secret%'
I read the Symfony4 best practices about the new DotEnv package.
Using environment variables, while far from being perfect, have many benefits over what we currently do. Environment variables are a more "standard" way of managing settings that depend on the environment (no need to manage a parameters.yml.dist for instance).
As suggested in best practices, I append these two line to .env
file:
TWITTER_CONSUMER_ID=XXXXXMyIdXXXXX
TWITTER_CONSUMER_SECRET=XXXXXMyTopSecretKeyXXXXX
But I encountered this error:
You have requested a non-existent parameter "twitter_consumer_id".
I tried with %kernel.twitter_consumer_id%
, %env.twitter_consumer_id%
, %env(TWITTER_CONSUMER_ID)%
with no more success.
The last test is returning this error message:
An exception has been thrown during the rendering of a template ("Environment variable not found: "TWITTER_CONSUMER_ID".").
How can I retrieve my ENV variables in a parameter file like hwi_oauth.yaml
?
.env
with theDotEnv
component? – Oogenesispublic\index.php
containsif (!getenv('APP_ENV')) { (new Dotenv())->load(__DIR__.'/../.env'); }
and this.env
was not loaded. It works now ! But the check is to ensure we don't use.env
in production. I don't understand how to use it in dev and still protect my prod... Is it secured to repace the test!getenv('APP_ENV')
by this one:in_array(getenv('APP_ENV'), ['dev','test'])
? – Assort