当您创建可重用的捆绑包时,我不知道通过作曲家做到这一点的方法。相反,您可以使用 symfony 的配置组件
基于文件配置您的包,并使这些参数成为必需,这样当您安装包时,用户必须提供这些参数。对于您的用例,它将类似于: config.yml
// in YourBundle\DependencyInjection\Configuration
public function getConfigTreeBuilder(){
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('your_bundle_alias');
$rootNode
->children()
->scalarNode('param1')->isRequired()->end()
->scalarNode('param1')->isRequired()->end()
->end();
}
// in YourBundle\DependencyInjection\YourExtension
public function load(array $configs, ContainerBuilder $container){
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
// ...
// get the parameters entered in the config file, and configured above
$param1 = $config['param1'];
$param2 = $config['param2'];
// inject them into my_service
$container->getDefinition('my_service')
->setArguments(array($param1, $param2));
// ...
}
以这种方式编写您的捆绑配置将强制用户在config.yml
文件中为您的捆绑提供一个条目,如下所示:
your_bundle_alias:
param1: the_users_value
param2: another_value