在 Symfony 4 项目的 .env 中使用 Amazon RDS Globals

Use Amazon RDS Globals in .env in a Symfony 4 project

我已经建立了一个 Symfony 4 项目并将其部署到 AWS。 数据库连接的凭据可通过服务器全局变量获得。有没有办法直接从 .env 文件访问这些变量?如果不是,在不使用硬编码凭据的情况下连接到数据库的最佳方法是什么?

基本上我想像这样定义我的连接变量,以便我可以将它们分配给 DATABASE_URL.

RDS_HOSTNAME=$_SERVER['RDS_HOSTNAME']

是的,您可以直接从 .env 文件访问这些变量。如下。通过这种方式,您应该安装 Dotenv Bundle

RDS_HOSTNAME=getenv("RDS_HOSTNAME")

但是,这种方法是最好的开发方法。当您 运行 投入生产时,他们推荐使用环境变量方法。

Symfony Dotenv can be used in any environment of your application: development, testing, staging and even production. However, in production it's recommended to configure real environment variables to avoid the performance overhead of parsing the .env file for every request.

https://symfony.com/doc/current/components/dotenv.html