如何在 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. 你可以在Symfony 文档中找到更多关于容器和依赖注入的信息

例如,如果这是您的命令服务:

class ExampleCommand extends Command
{
    private $writingData;

    public function __construct(WritingData $writingData)
    {
        $this->writingData = $writingData;
    }
}

然后你注入WritingDataExampleCommandservices.xml

<service id="SwagBasicExample\Command\ExampleCommand">
    <argument type="service" id="Swag\BasicExample\Service\WritingData"/>
    <tag name="console.command"/>
</service>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Shopware 6:如何获取自定义模块中的所有属性

如何在 Shopware 6 中创建可在我的 Twig 文件中使用的自定义 Javascript?

如何在 Shopware 6 中订阅 API 获取的数据?

Shopware 6:如何访问此数组中的类别以显示在我的自定义页面上

如何从Shopware 6中的类别实体获取类别URL?

Shopware 6:如何使用Shopware \ Core \ Content \ Category \ Event中可用的`CategoryIndexerEvent`

获取路线Shopware 6的参数

如何从Shopware 6中的产品流ID中获得产品?

Shopware 6:如何使用国家/地区列表添加到自定义插件配置单选

Shopware 6中自定义字段的继承

如何在 shopware 6 中访问订阅者中的请求数据

如何在shopware6中的订单概览中过滤掉前端的订单状态

如何在Controller中为Shopware 6产品添加新的crossSellings?

如何在Shopware 6中安装B2B Suite?

Shopware 6:如何在管理模块的Twig模板中访问配置参数

如何在 Shopware 6 中通过控制台启用维护模式

Shopware 6:在自己的自定义管理模块中获取PHP数据

在Shopware 6中通过php创建媒体

如何在Shopware中管理SEO网址?

如何扩展Shopware 6搜索,使其能够按自定义字段,制造商名称,产品ID等进行搜索?

如何使用Rest API在Shopware 6中获取产品的媒体文件?

如何借助 shopware 6 中的 API 使用 productName 获取产品数据?

Shopware 6 为自定义实体创建数据库条目

如何隐藏Shopware 6中“ sw-entity-listing”列表中的网格动作按钮?

如何转储 SQL 查询以用于 Shopware 6 中存储库搜索的调试目的?

如何在 Shopware 插件中获取产品列表

如何在Shopware 5项目网址中使用自定义字段?

Shopware 6 如何提高后台订单搜索的性能?

如何訪問 Shopware 6 店面的 Javascript 翻譯?