Symfony 如何从捆绑包的本地配置文件添加全局配置?
我有一个应用程序,其中业务逻辑被拆分为捆绑包,以便于管理和扩展。每个捆绑包都提供其实体、服务、模板(如果是UI捆绑包等) 实体应该注册到ORM以参与命令,如./bin/console-doctor:schema:update 我对包使用自定义文件夹结构,因此每个包中没有任何根实体文件夹。因此,我必须使用doctor.orm.mappings配置密钥注册实体 我希望每个bundle注册自己的实体,而不将对它们的引用放入全局配置中 因此,我在每个包中使用PrependExtensionInterface。我的prepend方法如下所示:Symfony 如何从捆绑包的本地配置文件添加全局配置?,symfony,Symfony,我有一个应用程序,其中业务逻辑被拆分为捆绑包,以便于管理和扩展。每个捆绑包都提供其实体、服务、模板(如果是UI捆绑包等) 实体应该注册到ORM以参与命令,如./bin/console-doctor:schema:update 我对包使用自定义文件夹结构,因此每个包中没有任何根实体文件夹。因此,我必须使用doctor.orm.mappings配置密钥注册实体 我希望每个bundle注册自己的实体,而不将对它们的引用放入全局配置中 因此,我在每个包中使用PrependExtensionInterfa
/**
* Allow an extension to prepend the extension configurations.
*
* @param ContainerBuilder $container
*/
public function prepend(ContainerBuilder $container)
{
$config = Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/config.yml'));
foreach ($config as $key => $configuration) {
$container->prependExtensionConfig($key, $configuration);
}
}
doctrine:
orm:
mappings:
thiris_cart_logic_auth_user:
type: annotation
dir: %kernel.root_dir%/../src/ThirisCart/Logic/AuthBundle/User/Model
alias: Category
prefix: ThirisCart\Logic\AuthBundle\User\Model
is_bundle: false
我的.yml看起来像这样:
/**
* Allow an extension to prepend the extension configurations.
*
* @param ContainerBuilder $container
*/
public function prepend(ContainerBuilder $container)
{
$config = Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/config.yml'));
foreach ($config as $key => $configuration) {
$container->prependExtensionConfig($key, $configuration);
}
}
doctrine:
orm:
mappings:
thiris_cart_logic_auth_user:
type: annotation
dir: %kernel.root_dir%/../src/ThirisCart/Logic/AuthBundle/User/Model
alias: Category
prefix: ThirisCart\Logic\AuthBundle\User\Model
is_bundle: false
除了一件事外,它工作得非常好。
看起来,我从中读取的配置。yml没有验证其正确性。它只是合并到全局配置中,没有进一步的问题
所以,问题是如何验证它?我通过查看Symfony源代码找到了答案 如果你深入观察,你会发现,第一个过程是,prepend被调用的地方。因此,附加的配置将得到验证 但请注意,Symfony会缓存配置。如果您在prepend方法中更改了代码,那么您也必须接触一些配置文件,或者只是使配置缓存无效,您的更改才能生效