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 : Too few arguments to function TryPlugin\Service\ReadingData::__construct(), 1 passed in /var/www/html/var/cache/dev_he0523cc28be2f689acaab5c325675d68/ContainerFt0wDoq/Shopware_Production_KernelDevDebugContainer.php on line 25455 and exactly 2 expected
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;
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
<?php declare(strict_types=1);
namespace TryPlugin\Storefront\Controller;
use Shopware\Core\Framework\Context;
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 $this->renderStorefront('@Storefront/storefront/page/content/index.html.twig', [
'products' => $meinData,
]);
}
}
Service.xml:
<service id="TryPlugin\Service\ReadingData">
<argument type="service" id="product.repository"/>
</service>
<!--ReadingDate From Controller-->
<service id="TryPlugin\Storefront\Controller\PageController" public="true">
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
<tag name="controller.service_arguments"/>
</service>
CodePudding user response:
You need to pass the 2nd argument in service.xml
Your class requires two arguments:
public function __construct(EntityRepositoryInterface $productRepository, Context $con) { //...
but only provides one in service.xml
:
<service id="TryPlugin\Service\ReadingData">
<argument type="service" id="product.repository"/>
<!-- Need argument for `Context $con` -->
</service>
Looking at the documentation, Context
does not appear to be autowired by default.
Therefore, you must inject the service yourself in service.xml
.
If you grow tired of all ways specifying the arguments in service.xml
, look into enabling and configuring autowire for ShopWare.
CodePudding user response:
This is unecessary in the first place.
Do the following in your controller and pass the context down:
namespace TryPlugin\Storefront\Controller;
use Shopware\Core\Framework\Context;
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, Context $context): Response
{
$meinData = $ReadingDatan->readData($context);
return $this->renderStorefront('@Storefront/storefront/page/content/index.html.twig', [
'products' => $meinData,
]);
}
}
This will work since there is a parameter resolver for controllers.