doumicheng6732 2017-08-25 19:49
浏览 120


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.

    firewall_names: [secured_area]
            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:

    firewall_names: [secured_area]
            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:


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?

  • 写回答

2条回答 默认 最新

  • dongqin1861 2017-08-25 20:25

    You need to load the .env file during your bootstrap process, in order for those environment variables to be available:

    (new DotEnv())->load(__DIR__ . '/../.env');

    You should plan to put secret keys in environment variables on development, staging, and production. How you do that depends, though. In development and staging, perhaps you use .env files, while on production you use Apache to inject.

    Personally, I always use .env files, and I keep a blank one in my repository. That way it's super simple to deploy, and there aren't any special cases.

    If you only want to use .env files in specific environments, you can do:

    if (in_array(getenv('APP_ENV'), [ 'dev', 'test' ])) {
        (new DotEnv())->load(__DIR__ . '/../.env');
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 qt中connect两个signal
  • ¥20 pix2pixHD运行测试命令时出现数据类型错误无法反向传播的问题
  • ¥15 python处理Excel符合条件的行自动填写数据分类
  • ¥15 汇编hook举例并讲解(通俗易懂,学习用)
  • ¥20 用c++语言模拟键盘电子琴设计
  • ¥15 STM32cubemx生成keil工程,有问题与正常的情况不同,求解!
  • ¥15 如何自动点击银行app的安全键盘,实现密码自动输入
  • ¥15 关于四边形重叠的问题
  • ¥15 用verilog语言设计一个简易的八音符电子琴,可通过按键输入来控制音响。演奏时可以选择是手演奏(由键盘输入)或自动演奏已存入的乐曲。能够自动演奏多首乐曲,且每首乐曲可重复演奏
  • ¥15 sap gui脚本每次到导出Excel的时候就停住不动。不会另存为。