如何 create/get Shopware 6 中的自定义服务实例
how to create/get an instance of an custom service in Shopware 6
出于某种原因,我不完全了解如何获取自定义服务的简单实例。
这是我到目前为止所遵循的文档:
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
一样。您可以在 the Symfony documentation.
中找到有关容器和依赖项注入的更多信息
例如,如果这是您的命令服务:
class ExampleCommand extends Command
{
private $writingData;
public function __construct(WritingData $writingData)
{
$this->writingData = $writingData;
}
}
然后你将 WritingData
注入到 services.xml
中的 ExampleCommand
:
<service id="SwagBasicExample\Command\ExampleCommand">
<argument type="service" id="Swag\BasicExample\Service\WritingData"/>
<tag name="console.command"/>
</service>
出于某种原因,我不完全了解如何获取自定义服务的简单实例。 这是我到目前为止所遵循的文档:
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
一样。您可以在 the Symfony documentation.
例如,如果这是您的命令服务:
class ExampleCommand extends Command
{
private $writingData;
public function __construct(WritingData $writingData)
{
$this->writingData = $writingData;
}
}
然后你将 WritingData
注入到 services.xml
中的 ExampleCommand
:
<service id="SwagBasicExample\Command\ExampleCommand">
<argument type="service" id="Swag\BasicExample\Service\WritingData"/>
<tag name="console.command"/>
</service>