如何向 symfony 会话添加额外的包 [英] How to add extra bag to symfony session
本文介绍了如何向 symfony 会话添加额外的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 symfony 会话中添加一个额外的包.
我在编译器传递中这样做:
公共函数进程(ContainerBuilder $container){$bag = new AttributeBag("my_session_attributes");$container->getDefinition("session")->addMethodCall("registerBag", [$bag]);}
但我收到一条消息异常:
<块引用><块引用>如果参数是对象或参数,则无法转储服务容器资源.
这是跟踪堆栈:
- 在 XmlDumper.php 第 379 行
- 在 XmlDumper.php 第 328 行中的 XmlDumper::phpToXml(object(AttributeBag))
- 在 XmlDumper.php 第 94 行中的 XmlDumper->convertParameters(array(object(AttributeBag)), 'argument', object(DOMElement))
- at XmlDumper->addMethodCalls(array(array('registerBag', array(object(AttributeBag)))), object(DOMElement)) in XmlDumper.php第183行
- 在 XmlDumper->addService(object(Definition), 'session', object(DOMElement)) 在 XmlDumper.php 第 272 行
- 在 XmlDumper.php 第 52 行中的 XmlDumper->addServices(object(DOMElement))
- 在 ContainerBuilderDebugDumpPass.php 第 34 行中的 XmlDumper->dump()
- 在 ContainerBuilderDebugDumpPass->process(object(ContainerBuilder)) 中Compiler.php 第 104 行
- 在 Compiler->compile(object(ContainerBuilder)) 在 ContainerBuilder.php 第 598 行
- 在 ContainerBuilder->compile() 在 Kernel.php 第 514 行
- 在 Kernel.php 第 133 行中的 Kernel->initializeContainer()
- 在 Kernel->boot() 在 Kernel.php 第 182 行
- 在 app_dev.php 第 29 行的内核->handle(object(Request))
如果我不能在服务定义中传递对象参数,我应该如何添加新包?
解决方案
好的,在发布问题后我有了一个想法,我认为这是一种解决方法,但它有效.
AttributeBag 也必须注册为服务:
公共函数进程(ContainerBuilder $container){$bagDefinition = 新定义();$bagDefinition->setClass(AttributeBag::class);$bagDefinition->addArgument("my_session_attributes");$bagDefinition->addMethodCall("setName", ["my_session_attributes"]);$bagDefinition->setPublic(false);$container->setDefinition("my_session_attributes_service", $bagDefinition);$container->getDefinition("session")->addMethodCall("registerBag", [new Reference("my_session_attributes_service")]);}
I want to add an extra bag to symfony session.
I do that in compiler pass:
public function process(ContainerBuilder $container)
{
$bag = new AttributeBag("my_session_attributes");
$container->getDefinition("session")
->addMethodCall("registerBag", [$bag]);
}
But i get an exception with message:
Unable to dump a service container if a parameter is an object or a resource.
Here is the trace stack:
- in XmlDumper.php line 379
- at XmlDumper::phpToXml(object(AttributeBag)) in XmlDumper.php line 328
- at XmlDumper->convertParameters(array(object(AttributeBag)), 'argument', object(DOMElement)) in XmlDumper.php line 94
- at XmlDumper->addMethodCalls(array(array('registerBag', array(object(AttributeBag)))), object(DOMElement)) in XmlDumper.php line 183
- at XmlDumper->addService(object(Definition), 'session', object(DOMElement)) in XmlDumper.php line 272
- at XmlDumper->addServices(object(DOMElement)) in XmlDumper.php line 52
- at XmlDumper->dump() in ContainerBuilderDebugDumpPass.php line 34
- at ContainerBuilderDebugDumpPass->process(object(ContainerBuilder)) in Compiler.php line 104
- at Compiler->compile(object(ContainerBuilder)) in ContainerBuilder.php line 598
- at ContainerBuilder->compile() in Kernel.php line 514
- at Kernel->initializeContainer() in Kernel.php line 133
- at Kernel->boot() in Kernel.php line 182
- at Kernel->handle(object(Request)) in app_dev.php line 29
How should I add new bag if I can't pass object arguments in service definitions?
解决方案
Ok, just after posting the question I had an idea which I consider a workaround but it works.
The AttributeBag has to be registered as service too:
public function process(ContainerBuilder $container)
{
$bagDefinition = new Definition();
$bagDefinition->setClass(AttributeBag::class);
$bagDefinition->addArgument("my_session_attributes");
$bagDefinition->addMethodCall("setName", ["my_session_attributes"]);
$bagDefinition->setPublic(false);
$container->setDefinition("my_session_attributes_service", $bagDefinition);
$container->getDefinition("session")
->addMethodCall("registerBag", [new Reference("my_session_attributes_service")]);
}
这篇关于如何向 symfony 会话添加额外的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文