Akénéo服务pim_catalog.saver.product是私有的,不能在我的软件包的Controller中使用

问题描述

根据文档https://docs.akeneo.com/4.0/manipulate_pim_data/product/save.html,我应该能够在捆绑软件的控制器中调用$saver = $this->get('pim_catalog.saver.product');,如下所示:

<?PHP
namespace XXX\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class ExportController extends Controller
{
    public function exportProduct($id): Response
    {
        $saver = $this->get('pim_catalog.saver.product');
        $saver->save($id);

    
        return new Response(
            '<html><body>foo</body></html>'
        );
    }

}

但是我收到此错误

[2020-11-05 13:44:58] request.Critical:未捕获的PHP异常Symfony \ Component \ DependencyInjection \ Exception \ ServiceNotFoundException:“在以下情况下,“ pim_catalog.saver.product”服务或别名已被删除或内联容器已编译。您应该将其公开,或者直接停止使用该容器,而改为使用依赖项注入。”在/var/www/html/pim/vendor/symfony/dependency-injection/Container.PHP行275 {“ exception”:“ [object](Symfony \ Component \ DependencyInjection \ Exception \ ServiceNotFoundException(code:0):” pim_catalog.saver.product“服务或别名在编译容器时已被删除或内联。您应该将其公开,或者直接停止使用该容器并改为使用依赖项注入。位于/ var / www / html / pim / vendor /symfony/dependency-injection/Container.PHP:275)“} []

因此,我想通过将services.YML文件添加到捆绑软件的配置目录(mybundle/Resources/config/services.YML)中来覆盖此服务的声明:

services:
  pim_catalog.saver.product:
    public: true
    priority: 999

但是它仍然不起作用。

根据Symfony 4的文档,也许应该创建一个Extension类。它应该与Akénéo的名字相同,但我找不到后者。

我该怎么办?

解决方法

错误消息中有一些提示。

“ pim_catalog.saver.product”服务或别名已被删除或 在编译容器时内联。您应该成功 公共,或停止直接使用容器并使用依赖项 代替

作为一种最佳实践,我不鼓励将其公开,因为您不是服务提供者,并且有更好,更轻松的方法来解决您的问题。因此,您留下第二种选择,这是我的拙见。

我不太了解Akeneo,但我确实了解Symfony和依赖注入。因此,我建议在控制器方法中使用参数typehint。

1。检查用于服务的自动装配类/接口“ pim_catalog.saver.product”

列出所有已定义的自动接线,并查找提及“ pim_catalog.saver.product”的那一

console debug:autowiring

或者仅检查名称中带有akeneo的所有自动装配的类/接口(可能)

console debug:autowiring akeneo

2。更新您的控制器代码以添加带有类型提示的参数

以下内容

<?php
namespace XXX\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

// I just picked that interface after looking it up in Akeneo documentation
// But there is another one that can be used if you want to save multiple
// products,so beware.
use Akeneo\Tool\Component\StorageUtils\Saver\SaverInterface;

class ExportController extends Controller
{
    public function exportProduct($id,SaverInterface $saver): Response
    {
        $saver->save($id);

        return new Response(
            '<html><body>foo</body></html>'
        );
    }

}

此过程记录在https://symfony.com/doc/4.4/controller.html#fetching-services

3。 (希望如此)享受工作代码!

;-)