如何在Symfony4结构的参数文件中检索环境变量? [英] How can I retrieve my environment variables in a parameter file in Symfony4 structure?

查看:111
本文介绍了如何在Symfony4结构的参数文件中检索环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I did a fresh Symfony installation by using Symfony Flex and the new skeleton belong to the next Symfony 4 directory structure.

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

I add and configure a first third-party bundle : HWIOAuthBundle. This bundle is used to connect via Twitter using two secret information.

我在config/packages/hwi_oauth.yaml文件中声明了我的consumer_idconsumer_secret.

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

我的应用程序运行正常.但是我无法在github上提交我的秘密!

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

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%'

我阅读了有关新DotEnv软件包的 Symfony4最佳做法.

I read the Symfony4 best practices about the new DotEnv package.

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

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).

按照最佳做法中的建议,我将这两行附加到.env文件中:

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:

您请求的参数"twitter_consumer_id"不存在.

You have requested a non-existent parameter "twitter_consumer_id".

我尝试使用%kernel.twitter_consumer_id%%env.twitter_consumer_id%%env(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:

在呈现模板的过程中引发了异常(未找到环境变量:"TWITTER_CONSUMER_ID".).

An exception has been thrown during the rendering of a template ("Environment variable not found: "TWITTER_CONSUMER_ID".").

如何在hwi_oauth.yaml这样的参数文件中检索我的ENV变量?

How can I retrieve my ENV variables in a parameter file like hwi_oauth.yaml?

推荐答案

为了使这些环境变量可用,您需要在引导过程中加载.env文件:

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');

您应该计划在开发,登台和生产时将密钥放入环境变量中. 您的操作方式取决于.在开发和暂存中,也许您使用.env文件,而在生产中则使用Apache注入.

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.

我个人总是使用.env文件,并且在存储库中保留一个空白文件.这样,部署起来超级简单,而且没有任何特殊情况.

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.

如果您只想在特定环境中使用.env文件,则可以执行以下操作:

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');
}

这篇关于如何在Symfony4结构的参数文件中检索环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆