函数ReadingData::__construct()的参数太少,传入了1..KernelDevDebugContai



我创建了一个服务来从数据库中读取数据。为了实现这一点,我想制作一个控制器,并抛出我想首先调用的控制器ReadingDataService

错误消息:

函数TryPluginServiceReadingData::__construct()的参数太少,在第25455行的/var/www/html/var/cache/dev_he0523cc28be2f689acaab5c325675d68/ContainerFt0wDoq/Shopware_Production_KernelDevDebugContainer.php中传递了1个,而恰好需要2个

代码:
ReadingData.php

class ReadingData
{
private EntityRepositoryInterface $productRepository;
private Context $con;
public function __construct(EntityRepositoryInterface $productRepository, Context $con)
{
$this->productRepository = $productRepository;
$this->con = $con;
}
public function readData(): void
{
$criteria1 = new Criteria();
$products = $this->productRepository->search($criteria1, $this->con)->getEntities();
}
}

PageController.php

/**
* @RouteScope (scopes={"storefront"})
*/
class PageController extends StorefrontController
{

/**
* @Route("/examples", name="examples", methods={"GET"})
*/
public function showExample(ReadingData $ReadingDatan): Response
{
$meinData = $ReadingDatan->readData();
return $this->renderStorefront('@Storefront/storefront/page/content/index.html.twig', [
'products' => $meinData,
]);
}
}

Service.xml:

<service id="TryPluginServiceReadingData">
<argument type="service" id="product.repository"/>
</service>
<!--ReadingDate From Controller-->
<service id="TryPluginStorefrontControllerPageController" public="true">
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
<tag name="controller.service_arguments"/>
</service> 

您需要在service.xml中传递第二个参数

您的类需要两个参数:

public function __construct(EntityRepositoryInterface $productRepository, Context $con) { //...

但仅在service.xml:中提供一个

<service id="TryPluginServiceReadingData">
<argument type="service" id="product.repository"/>
<!-- Need argument for `Context $con` -->
</service>

查看文档,默认情况下,Context似乎不是自动连接的。

因此,您必须在service.xml中自己注入服务。

如果您厌倦了在service.xml中指定参数的所有方法,请考虑为ShopWare启用和配置autowire。

这首先是不必要的。

在控制器中执行以下操作并向下传递上下文:

namespace TryPluginStorefrontController;

use ShopwareCoreFrameworkContext;
use ShopwareCoreFrameworkDataAbstractionLayerEntityRepositoryInterface;
use ShopwareCoreFrameworkRoutingAnnotationLoginRequired;
use ShopwareCoreFrameworkRoutingAnnotationRouteScope;
use ShopwareCoreSystemSalesChannelSalesChannelContext;
use ShopwareStorefrontControllerStorefrontController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentHttpFoundationJsonResponse;
use TryPluginServiceReadingData;

/**
* @RouteScope (scopes={"storefront"})
*/
class PageController extends StorefrontController
{
/**
* @Route("/examples", name="examples", methods={"GET"})
*/
public function showExample(ReadingData $ReadingDatan, Context $context): Response
{
$meinData = $ReadingDatan->readData($context);
return $this->renderStorefront('@Storefront/storefront/page/content/index.html.twig', [
'products' => $meinData,
]);
}
}

这将起作用,因为控制器有一个参数解析器。

相关内容

  • 没有找到相关文章

最新更新