symfony - 编辑控制器中的 config.yml 文件
问题描述
出于某些安全原因,每个客户在我的 Symfony 3.4 应用程序中都有自己的数据库,这里是我的 config.yml 文件的示例:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_pgsql
host: 127.0.0.1
port: null
dbname: default
user: user
password: password
charset: UTF8
customer1:
driver: pdo_pgsql
host: 127.0.0.1
port: null
dbname: customer1
user: user
password: password
charset: UTF8
customer2:
driver: pdo_pgsql
host: 127.0.0.1
port: null
dbname: customer2
user: user
password: password
charset: UTF8
和 ORM :
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
connection: default
mappings:
BackBundle: ~
BackBundle: ~
customer1:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: false
connection: customer1
mappings:
BackBundle: ~
customer2:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: false
connection: customer2
mappings:
BackBundle: ~
我可以得到 config.yml 文件
$value = Yaml::parseFile('/pathtofile/app/config/config.yml');
$value 包含一个数组,但即使我使用 array_push() 结果也不可读
我的问题:
首先,当管理员在我想执行之后创建一个新用户时,我想在学说中创建一个新的数据库连接,并在 config.yml 文件中自动创建一个新的 orm 配置,doctrine:database:create --connection=dbname
并且doctrine:schema:update --force --em=emname
所有这些都在控制器中,而无需手动编辑 config.yml 文件
谢谢
解决方案
一个解决方案是用新生成的文件替换actuel config.yml。
像这样 :
use Symfony\Component\Yaml\Yaml;
$value = Yaml::parseFile('/pathtofile/app/config/config.yml');
$value["database_host"] = "10.2.2.1"
file_put_contents('/pathtofile/app/config/config.yml', $value);
推荐阅读
- azure-devops - Azure Function 的 Azure Devops CI 无法构建。奇怪的编译错误
- grails - 用 jquery-ui datepicker 替换 grails datepicker
- excel - 如何获取 GetSystemTimePreciseAsFileTime/“文件时间的 cLngLng”的微秒部分
- firebase - Apple 登录导致 FIRAuthErrorUserInfoNameKey=ERROR_EMAIL_ALREADY_IN_USE(代码 = 17007)
- c# - 在 C# 中,如何在不延长负值的情况下使用零填充格式化整数?
- c# - 有没有办法在 WPF 中求和或平均属性?
- mysql - Mysql Join 查询报表
- ruby-on-rails - 如何将 React 应用程序与使用 webpack 配置创建的 Ruby on Rails 分开?
- javascript - 当我调整窗口大小时,SVG图标消失
- webview - Oracle Webview SDK(JavaScript 的演示服务器)无法读取文件:docx、pptx、xlsx、。像基本文件一样