在services.xml中注册我的服务和组件有问题
当我试图注册组件时,这就是我收到的结果。
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException:
致命错误:未命名的服务"task.task_subscriber“依赖于不存在的服务"task.service.random_product”。在/var/www/shopware/vendor/symfony/dependency-injection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php:31\nStack trace:\n#0 /var/www/shopware/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php(60):Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Object(Symfony\Component\DependencyInjection\Reference),false)\n#1 /var/www/shopware/vendor/symfony/dependency-injection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php(28):Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Array,中/var/www/shopware/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php(67):Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Array)\n#3 /var/www/shopware/vendor/symfony/dependency-injection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php中的\n#2 /var (第31行)
这是我的services.xml
<services>
<service id="task.task_subscriber" class="Task\Subscriber\TaskSubscriber">
<argument>%task.plugin_name%</argument>
<argument>%task.plugin_dir%</argument>
<argument type="service" id="task.components.random_product" />
<argument type="service" id="shopware.plugin.cached_config_reader" />
<tag name="shopware.event_subscriber" />
</service>
<service id="task.random_product" class="Task\Components\RandomProduct">
<argument type="service" id="dbal_connection"/>
</service>
</services>谁能给我解释一下有什么问题吗?
发布于 2022-01-11 14:41:14
向id="task.random_product"注册类RandomProduct
因此,您需要在其他服务中使用相同的id。
所以这应该是可行的
<services>
<service id="task.task_subscriber" class="Task\Subscriber\TaskSubscriber">
<argument>%task.plugin_name%</argument>
<argument>%task.plugin_dir%</argument>
<argument type="service" id="task.random_product" />
<argument type="service" id="shopware.plugin.cached_config_reader" />
<tag name="shopware.event_subscriber" />
</service>
<service id="task.random_product" class="Task\Components\RandomProduct">
<argument type="service" id="dbal_connection"/>
</service>
</services>在类TaskSubscriber中,可以将RandomProduct传递给
代码中参数和我们依赖项注入的__construct按相同的顺序
所以看起来会是这样
<?php
namespace Task\Subscriber;
use Task\Components\RandomProduct;
class TaskSubscriber implements SubscriberInterface
{
private $pluginName;
private $pluginDirectory;
private $randomProduct;
private $config;
public function __construct(
$pluginName,
$pluginDirectory,
RandomProduct $randomProduct,
$config
)
{
$this->pluginName = $pluginName;
$this->pluginDirectory = $pluginDirectory;
$this->randomProduct = $randomProduct;
$this->config = $config;
}https://stackoverflow.com/questions/63955712
复制相似问题