问题描述
我通过使用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_id
和consumer_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.
按照最佳做法中的建议,我将这两行附加到.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:
我尝试使用%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:
如何在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结构的参数文件中检索环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!