i created a Service to reading data from the Datebase, in order to achieve that, i want to make a Controller and throw this controller i want to call first the ReadingData Service. however get the Bug : Could not resolve argument $ReadingDatan of "TryPlugin\Storefront\Controller\PageController::showexample()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?
my codes :
ReadingData.php
<?php declare(strict_types=1);
namespace TryPlugin\Service;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
class ReadingData
{
private EntityRepositoryInterface $productRepository;
public function __construct(EntityRepositoryInterface $productRepository)
{
$this->productRepository = $productRepository;
}
public function readData(Context $context): void
{
$products = $this->productRepository->search(new Criteria(), $context);
}
/* public function readData(Context $context): void
{
$product = $this->productRepository->search(new Criteria([$myId]), $context)->first();
}*/
}
PageController.php
<?php declare(strict_types=1);
namespace TryPlugin\Storefront\Controller;
use DateTime;
use DateTimeInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use TryPlugin\Service\ReadingData;
/**
* @RouteScope (scopes={"storefront"})
*/
class PageController extends StorefrontController
{
/**
* @Route("/examples", name="examples", methods={"GET"})
*/
public function showExample(ReadingData $ReadingDatan): Response
{
$meinData = $ReadingDatan->readData();
return new Response($meinData);
}
}
Service.xml:
<service id="TryPlugin\Storefront\Controller\PageController" public="true">
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
<!--ReadingDate From Controller-->
<service id="TryPlugin\Storefront\Controller\ProductController" public="true">
<argument type="service" id="product.repository"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
When you want to use autowire just add the tag how the error says
<service id="TryPlugin\Storefront\Controller\PageController" public="true">
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
<tag name="controller.service_arguments"/>
</service>
<service id="TryPlugin\Storefront\Controller\ProductController" public="true">
<argument type="service" id="product.repository"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
<tag name="controller.service_arguments"/>
</service>