我有一个使用我的附加包的项目。此捆绑包连接到其他数据库,我需要配置另一个数据库。
我希望在2个配置文件中建立此连接。
主配置:
# ROOT/app/config/config.yml:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
捆绑配置:
# src/SecondBundle/Resources/config/config.yml
doctrine:
dbal:
connections:
secondBundle:
driver: "%secondBundle.database_driver%"
host: "%secondBundle.database_host%"
port: "%secondBundle.database_port%"
dbname: "%secondBundle.database_name%"
user: "%secondBundle.database_user%"
password: "%secondBundle.database_password%"
charset: UTF8
捆绑扩展文件:
class SecondBundleExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('config.yml');
}
}
在我看来,一切看起来都不错,但是当我试图运行时,我已经沟通了:
没有可以加载“doctrine”
的配置的扩展程序
答案 0 :(得分:4)
您可以使用SecondBundle
声明特定于您的捆绑包的第二个驱动程序(在您的示例中名为PrependExtensionInterface
。
首先将config.yml
中的SecondBundle
文件重命名为doctrine.yml
(或任何其他非config.yml
的名称。
立即更改您的SecondBundleExtension
课程:
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;
// ...
class SecondBundleExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
// ...
}
public function prepend(ContainerBuilder $container)
{
$yamlParser = new YamlParser();
try {
$doctrineConfig = $yamlParser->parse(
file_get_contents(__DIR__.'/../Resources/config/doctrine.yml')
);
} catch (ParseException $e) {
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
}
$container->prependExtensionConfig('doctrine', $doctrineConfig['doctrine']);
}
}
现在,您启用捆绑包时会自动注册secondBundle
连接。
答案 1 :(得分:2)
您可以将额外的配置添加到app/config/config.yml
中的导入中,以便将其合并为完整的config
。
应用程序/配置/ config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: '@SecondBundle/Resources/config/config.yml' }
由于版本3.0以来a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
这一事实而更新了引号。