Symfony 2 YAML 传递数组
我想知道在哪里可以获取有关 symfony2 yaml 配置中的特殊语法(如 @somevar
或 %somevar%
)的更多信息?
例如,使用 @
定义对服务的调用,这就是我们将依赖项传递给服务的方式。另一方面,%somevar%
指的是名称为 somevar
的已定义参数的值。
因此,如果我这样做:
parameters:
custom: "some value"
another: %custom%
那么 another
将填充 custom
的值,在我的例子中是“某个值”。我的问题是,这些关系记录在哪里?
我的特殊需求是能够引用数组的元素,例如 %somevar[somekey]%
,但该语法不起作用。
提前致谢!
编辑:我发现了这个:完整的合并键支持。 完全支持引用、别名和完整的合并键。不要通过
在 yaml 文档中引用常见配置位来重复自己。,但没有关于它的进一步文档。
I wonder where can I get more information about special syntax like @somevar
or %somevar%
in symfony2's yaml configuration?
For example, using @
defines a call to a service, that is how we pass dependencies to services. %somevar%
on the other hand refers to the value of an already defined parameter with the name somevar
.
So, if I do:
parameters:
custom: "some value"
another: %custom%
then another
will be populated with the value of custom
, which in my case is "some value". My question is, where are these relations documented?
My particular need is to be able to reference an element of an array, something like %somevar[somekey]%
, but that syntax doesn't work.
Thanks in advance!
EDIT: I found this: Full merge key support.
Full support for references, aliases, and full merge key. Don't repeat yourself by referencing common configuration bits.
in the yaml docs, but no furthur documentation about it..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在搜索的并不是真正关于 Yaml 本身,而是关于依赖注入容器的 Yaml 加载器。
如果您搜索有关它的文档,这里是旧组件(v1)的文档: http://components.symfony-project.org/dependency-injection/trunk/book/05-Service-Description
Symfony2 附带了一个新组件(基于相同的原则)。您可以在这里找到官方文档: http://symfony.com/doc /current/book/service_container.html#service-parameters
关于您的问题,您无法访问 DI 参数的键,您必须手动展平。
您可以使用 DI 扩展来满足您的需求,以某些捆绑包为例: https://github.com/symfony/AsseticBundle/blob/master/DependencyInjection/AsseticExtension.php#L54(也许不是最好的例子)。
What you are searching for is not really about Yaml itself, but about the Yaml loader of the Dependency Injection container.
If you search docs about it, here are the ones for the old component (v1): http://components.symfony-project.org/dependency-injection/trunk/book/05-Service-Description
Symfony2 comes with a new component (based on the same principles). You can find the official docs here: http://symfony.com/doc/current/book/service_container.html#service-parameters
Concerning your problem, you cannot access to keys of DI parameters, you have to flatten then manually.
You could use a DI extension to fit your need, take example on some bundles like: https://github.com/symfony/AsseticBundle/blob/master/DependencyInjection/AsseticExtension.php#L54 (maybe not the best example).