doumicheng6732
2017-08-25 19:49
浏览 115
已采纳

如何在Symfony4结构的参数文件中检索环境变量?

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?

图片转代码服务由CSDN问答提供 功能建议

我使用Symfony Flex进行了全新的Symfony安装,新的骨架属于下一个Symfony 4目录结构。

我添加并配置了第一个第三方软件包: HWIOAuthBundle 。 该捆绑包用于通过Twitter使用两个秘密信息进行连接。

我在中声明了 consumer_id consumer_secret config / packages / hwi_oauth.yaml 文件。

  hwi_oauth:
 firewall_names:[secured_area] 
 resource_owners:
 twitter:
 type:twitter \  n client_id:XXXXXMyIdXXXXX 
 client_secret:XXXXXMyTopSecretKeyXXXXX 
   
 
 

我的应用程序运行正常。 但是我不能在github上提交我的秘密!

我想要一个像这样的 hwi_oauth.yaml 文件:

  hwi_oauth:
 firewall_names:[secured_area] 
 resource_owners:
 twitter:
 type:twitter 
 client_id:'%twitter_consumer_id%'
 client_secret:'%twitter_consumer_secret%'
    
 
 

我阅读了 Symfony4最佳做法 关于新的DotEnv包。

使用环境变量虽然远非完美,但与我们目前的工作相比有很多好处。 环境变量是一种更“标准”的方式来管理依赖于环境的设置(例如,不需要管理parameters.yml.dist)。

As 在最佳实践建议,我追加这两个线为 .ENV 文件:

 <代码> TWITTER_CONSUMER_ID = XXXXXMyIdXXXXX 
TWITTER_CONSUMER_SECRET = XXXXXMyTopSecretKeyXXXXX 
  <  / pre> 
 
 

但是我遇到了这个错误:

您已经请求了一个不存在的参数“twitter_consumer_id”。

我尝试使用%kernel.twitter_consumer_id%%env.twitter_consumer_id%%env(TWITTER_CONSUMER_ID)%< / code>没有更多成功。

最后一个测试返回此错误消息:

在抛出期间抛出异常 模板的渲染(“未找到环境变量:”TWITTER_CONSUMER_ID“。”)。

如何在参数文件中检索ENV变量 喜欢 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');
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题