这是我的app / config.yml
sifast_symash:
scrollers:
scroller1:
name: scroller1
page_range: 5
template:
main_page: SifastTestBundle:Default:index.html.twig
page: SifastTestBundle:Default:show.html.twig
scroller2:
name: scroller2
page_range: 8
template:
main_page: SifastTestBundle:Default:index.html.twig
page: SifastTestBundle:Default:show.html.twig
我的包service.yml
parameters:
sifast_symash_scroller.class: Sifast\Bundle\SymashBundle\Manager\Scroll
scroller : scroller1
services:
sifast_symash_scroller:
class : %sifast_symash_scroller.class%
scope: prototype
arguments:
name: %sifast_symash_scroller.name%
page_range: %sifast_symash_scroller.page_range%
template:
main_page : %sifast_symash_scroller.template.main_page%
page : %sifast_symash_scroller.template.page%
配置类:
public function getConfigTreeBuilder()
{
$builder = new TreeBuilder();
$builder->root('sifast_symash')
->children()
->arrayNode('scrollers')
->prototype('array')
->children()
->scalarNode('name')->end()
->integerNode('page_range')->defaultValue(5)->end()
->arrayNode('template')
->children()
->scalarNode('main_page')->defaultValue("SifastTestBundle:Default:index.html.twig")->end()
->scalarNode('page')->defaultValue("SifastTestBundle:Default:show.html.twig")->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
return $builder;
}
SifastSymashExtension类
$scroller = $config['scrollers'][$container->getParameter("scroller")];
$container->setParameter('sifast_symash_scroller.name', $scroller['name']);
$container->setParameter('sifast_symash_scroller.page_range', $scroller['page_range']);
$container->setParameter('sifast_symash_scroller.template',$scroller['template']);
$container->setParameter('sifast_symash_scroller.template.main_page',$scroller['template']['main_page']);
$container->setParameter('sifast_symash_scroller.template.page',$scroller['template']['page']);
现在,在我的控制器中,我想选择scroller2配置,我想设置参数滚动条,但我不能,因为容器已经加载了:
$this->container->setParameter("sifast_symash_scroller.scroller", "scroller1");
$scroll = $this->get("sifast_symash_scroller");
答案 0 :(得分:0)
在Extension类中,您必须手动声明滚动条服务定义:
use Symfony\DependencyInjection\DependencyInjection\DefinitionDecorator;
// ...
public function load(array $configs, ContainerBuilder $container)
{
// ...
$scrollers = $config['scrollers'];
foreach ($scrollers as $name => $scroller) {
// Inherit the definition of "sifast_symash.scroller".
$definition = new DefinitionDecorator('sifast_symash.scroller');
// Inject the configured values in the constructor of the class "Scroller".
$definition->addArgument($scroller['name']); // First argument
$definition->addArgument($scroller['page_range']); // Second argument
$definition->addArgument($scroller['template']); // Third argument
// Set a new definition suffixed with the configured name of the scroller.
$container->setDefinition(
sprintf('sifast_symash.scroller.%s', $name),
$definition
);
}
}
以下是相应的services.yml
:
parameters:
sifast_symash.scroller.class: Sifast\Bundle\SymashBundle\Manager\Scroll
services:
sifast_symash.scroller:
class : %sifast_symash.scroller.class%
abstract: true
注意强>
我更改了“ sifast_symash。”中的所有“ sifast_symash _ ”以遵循Symfony2的推荐。
现在,您的每个滚动条都有一项服务:sifast_symash.scroller.scroller1
和sifast_symash.scroller.scroller2
。
你真的应该避免使用scope: prototype
作为服务的对立面。首选的是模式提供者(提供特定类实例的服务)。这是一个link,显示了这种模式在Symfony中使用的位置和原因(这里是一个独特的实例)。