出于某种原因,我不完全了解如何获取自定义服务的简单实例。这是我迄今为止遵循的文档:
https://developer.shopware.com/docs/guides/plugins/plugins/framework/data-handling
class WritingData
{
private $productRepository;
private $taxRepository;
public function __construct(EntityRepositoryInterface $productRepository, EntityRepositoryInterface $taxRepository)
{
$this->productRepository = $productRepository;
$this->taxRepository = $taxRepository;
}
}
services.xml 也被设置
<?xml version="1.0" ?>
<services>
<service id="Swag\BasicExample\Service\WritingData" >
<argument type="service" id="product.repository"/>
<argument type="service" id="tax.repository"/>
</service>
</services>
</container>
问题是:如何在我的命令服务中获取 WrtingData 的实例?
您必须将自定义服务注入到命令服务中,就像将存储库注入WritingData
. 你可以在Symfony 文档中找到更多关于容器和依赖注入的信息。
例如,如果这是您的命令服务:
class ExampleCommand extends Command
{
private $writingData;
public function __construct(WritingData $writingData)
{
$this->writingData = $writingData;
}
}
然后你注入WritingData
到ExampleCommand
中services.xml
:
<service id="SwagBasicExample\Command\ExampleCommand">
<argument type="service" id="Swag\BasicExample\Service\WritingData"/>
<tag name="console.command"/>
</service>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句