【发布时间】:2022-12-17 05:02:36
【问题描述】:
该文档仅显示您可以 pass the SystemConfigService as a parameter 到另一个服务。
是否也可以直接从插件配置中传递值?
问题背景:我想直接初始化一个外部组件的实例。但这需要固定参数作为字符串。或者,否则将不得不编写某种工厂。
【问题讨论】:
标签: dependency-injection plugins shopware6
该文档仅显示您可以 pass the SystemConfigService as a parameter 到另一个服务。
是否也可以直接从插件配置中传递值?
问题背景:我想直接初始化一个外部组件的实例。但这需要固定参数作为字符串。或者,否则将不得不编写某种工厂。
【问题讨论】:
标签: dependency-injection plugins shopware6
从今天开始,不可能在服务中注入特定的 system_config 值。
【讨论】:
嗯,是可以的。我自己没有直接这样做,但 99% 相信它会起作用。您可能需要尝试一下。
在 services.xml 中,您可以使用 symfony 表达式。
<argument type="expression">service('ShopwareCoreSystemSystemConfigSystemConfigService').get('SwagBasicExample.config.example')</argument>
您可能需要找到 ShopwareCoreSystemSystemConfigSystemConfigService 类的别名。还要检查 Symfony 文档,你可以用它做更多的事情!
我自己传递了一个数组作为参数,但使用自定义类作为配置 getter,如下所示:
<argument type="expression">
{
"shop_is_active": service('config_bridge').get('isActive'),
"customer_number": service('config_bridge').get('customerNumber'),
"shop_number": service('config_bridge').get('shopNumber'),
"apikey": service('config_bridge').get('apiKey')
}
</argument>
不是严格必要的,因为 Shopware 已经需要它,但是将要求添加到您的插件编辑器文件中始终是一个好习惯:
"require": {
...,
"symfony/expression-language": "~5.3.0|~5.4.0"
},
【讨论】: