无法自动装配服务AbstractController::setContainer(),ContainerInterface不存在。

huangapple go评论69阅读模式
英文:

Cannot autowire service AbstractController::setContainer() ContainerInterface does not exists

问题

I'm currently trying to update my Shopware 6 plugin to Version 6.5. This new version update, increased the php and internal Symfony version. With this, I receive the following error when I try to initialize the plugin:

Cannot autowire service "**\Controller\*ProductController": argument "$container" of method "Symfony\Bundle\FrameworkBundle\Controller\AbstractController::setContainer()" references interface "Psr\Container\ContainerInterface" but no such service exists. Available autowiring aliases for this interface are: "$parameterBag".

I use an abstract class for my controller:

use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

abstract class AMyController extends AbstractController
{

    public function __construct(private readonly SystemConfigService $systemConfigService)
    {
    }
    //...
}

My controller:

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\System.SystemConfig\SystemConfigService;
use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder;
use Symfony\Component\Routing\Annotation\Route;

#[Route(defaults: ['_routeScope' => ['api']])]
class MyProductController extends AMyController 
{

    public function __construct(
        private readonly EntityRepository $orderRepository,
        private readonly EntityRepository $orderLineItemRepository,
        private readonly EntityRepository $productRepository,
        private readonly RequestCriteriaBuilder $requestCriteriaBuilder,
        private readonly SystemConfigService $systemConfigService
    )
    {
        parent::__construct($systemConfigService);
    }
    //...
}

In my services.xml (Resources/config/services.xml) file I have this service registered like this:

    <services>
        <service
                id="Symfony\Component\DependencyInjection\ContainerInterface"
                alias="service_container"
        />

        <service id="MyPluginName\Controller\MyProductController" public="true">
            <argument type="service" id="order.repository"/>
            <argument type="service" id="order_line_item.repository"/>
            <argument type="service" id="product.repository"/>
            <argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder"/>
            <argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>

            <call method="setContainer">
                <argument type="service" id="service_container"/>
            </call>
        </service>

composer.json

{
    "autoload": {
        "psr-4": {
            "MyPrefix\\MyPluginName\\": "src/"
        }
    },
    "require": {
        "shopware/core": ">=v6.5.0",
        "shopware/administration": ">=v6.5.0"
    }

}

Versions used:

  • Shopware 6.5.2.1
  • Symfony 6.3.0
  • PHP 8.1.20

What I did so far:

  • I used the code refactor tool, and now use the Route Annotations #[Route(defaults: ['_routeScope' => ['api']])]
  • I used the services.xml file to manually define all services
  • Cleared cache with ./bin/console cache:clear
英文:

I'm currently trying to update my Shopware 6 plugin to Version 6.5. This new version update, increased the php and internal Symfony version. With this, I receive the following error when I try to initialize the plugin:

Cannot autowire service &quot;**\Controller\*ProductController&quot;: argument &quot;$container&quot; of method
&quot;Symfony\Bundle\FrameworkBundle\Controller\AbstractController::setContainer()&quot; references
interface &quot;Psr\Container\ContainerInterface&quot; but no such service exists.
Available autowiring aliases for this interface are: &quot;$parameterBag&quot;.

I use a abstract class for my controller:

use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

abstract class AMyController extends AbstractController
{

    public function __construct(private readonly SystemConfigService $systemConfigService)
    {
    }
//...
}

My controller:

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder;
use Symfony\Component\Routing\Annotation\Route;

#[Route(defaults: [&#39;_routeScope&#39; =&gt; [&#39;api&#39;]])]
class MyProductController extends AMyController 
{

    public function __construct(
        private readonly EntityRepository $orderRepository,
        private readonly EntityRepository $orderLineItemRepository,
        private readonly EntityRepository $productRepository,
        private readonly RequestCriteriaBuilder $requestCriteriaBuilder,
        private readonly SystemConfigService $systemConfigService
    )
    {
        parent::__construct($systemConfigService);
    }
//...
}

In my services.xml (Resources/config/services.xml) file I have this service registered like this:

    &lt;services&gt;
        &lt;service
                id=&quot;Symfony\Component\DependencyInjection\ContainerInterface&quot;
                alias=&quot;service_container&quot;
        /&gt;

        &lt;service id=&quot;MyPluginName\Controller\MyProductController&quot; public=&quot;true&quot;&gt;
            &lt;argument type=&quot;service&quot; id=&quot;order.repository&quot;/&gt;
            &lt;argument type=&quot;service&quot; id=&quot;order_line_item.repository&quot;/&gt;
            &lt;argument type=&quot;service&quot; id=&quot;product.repository&quot;/&gt;
            &lt;argument type=&quot;service&quot; id=&quot;Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder&quot;/&gt;
            &lt;argument type=&quot;service&quot; id=&quot;Shopware\Core\System\SystemConfig\SystemConfigService&quot;/&gt;

            &lt;call method=&quot;setContainer&quot;&gt;
                &lt;argument type=&quot;service&quot; id=&quot;service_container&quot;/&gt;
            &lt;/call&gt;
        &lt;/service&gt;

composer.json

{
    &quot;autoload&quot;: {
        &quot;psr-4&quot;: {
            &quot;MyPrefix\\MyPluginName\\&quot;: &quot;src/&quot;
        }
    },
    &quot;require&quot;: {
        &quot;shopware/core&quot;: &quot;&gt;=v6.5.0&quot;,
        &quot;shopware/administration&quot;: &quot;&gt;=v6.5.0&quot;
    }

}

Versions used:

  • Shopware 6.5.2.1
  • symfony 6.3.0
  • php 8.1.20

What I did so far:

  • I used the code refactor tool, and now use the Route Annotations #[Route(defaults: [&#39;_routeScope&#39; =&gt; [&#39;api&#39;]])]
  • I used the services.xml file to manually define all services
  • Cleared cache with ./bin/console cache:clear

答案1

得分: 0

我成功修复了这个行为,方法是明确加载配置文件:

class MyPlugin extends Plugin
{

    public function build(ContainerBuilder $container): void
    {
        parent::build($container);

        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/Core/Content/DependencyInjection'));
        $loader->load('controller.xml'); // 与我的 services.xml 内容相同
    }
}

重要的是,服务定义中包含以下标签:

<service id="MyPluginName\Controller\MyProductController" public="true">
  <!-- 所有参数 ... -->
  <call method="setContainer">
     <argument type="service" id="service_container"/>
  </call>
</service>

确实,services.xml 在此之前没有被加载。

英文:

I was able to fix this behaviour by specifically loading the config file:

class MyPlugin extends Plugin
{

    public function build(ContainerBuilder $container): void
    {
        parent::build($container);

        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . &#39;/Core/Content/DependencyInjection&#39;));
        $loader-&gt;load(&#39;controller.xml&#39;); // same content as my services.xml
    }
}

It's important that the service definitions include the following tag:

&lt;service id=&quot;MyPluginName\Controller\MyProductController&quot; public=&quot;true&quot;&gt;
  &lt;!-- all arguments ... --&gt;
  &lt;call method=&quot;setContainer&quot;&gt;
     &lt;argument type=&quot;service&quot; id=&quot;service_container&quot;/&gt;
  &lt;/call&gt;
&lt;/service&gt;

The services.xml indeed wasn't loaded before.

huangapple
  • 本文由 发表于 2023年7月18日 16:15:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定