I'm trying to make a custom api endpoint in shopware 6 to get an order details as according to the order Id but when i add some service to the controller its shows non-existent service even when i add those service which are passed to constructor as argument in service.xml.
What are the reasons behind getting this error ?
My files;
src/Storefront/Controller/SomethingController.php
<?php declare(strict_types=1);
namespace Swag\PluginName\Storefront\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Swag\PluginName\Service\OrderService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Shopware\Core\Framework\Context;
/**
*
* @Route(defaults={"_routeScope"={"api"}})
*/
class SomethingController extends AbstractController
{
public $orderService;
public function __construct(
OrderService $orderService
)
{
$this->orderService = $orderService;
}
/**
* @Route("/api/orderconfig/{orderId}", name="api.something.orderconfig", methods={"GET"})
* @param string $orderId
* @return JsonResponse
*/
public function orderConfig(string $orderId): JsonResponse
{
$order = $this->orderService->getOrder($orderId, Context::createDefaultContext());
return $order;
}
}
src/Service/OrderService.php
<?php
namespace Swag\PluginName\Service;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class OrderService
{
/**
* @var EntityRepositoryInterface
*/
private $orderRepository;
/**
* @var LoggerInterface
*/
private $logger;
/**
* OrderRepository Constructor
*
* @param EntityRepositoryInterface $orderRepository
* @param LoggerInterface $logger
*/
public function __construct(
EntityRepositoryInterface $orderRepository,
LoggerInterface $logger
)
{
$this->orderRepository = $orderRepository;
$this->logger = $logger;
}
/**
* @param string $orderId
* @param Context $context
* @param array $associations
* @return OrderEntity|null
*/
public function getOrder(string $orderId, Context $context, array $associations = []): ?OrderEntity
{
$order = null;
try {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $orderId));
/** @var OrderEntity $order */
$order = $this->getOrderByCriteria($criteria, $context, $associations);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), [$e]);
}
return $order;
}
public function getOrderByCriteria(Criteria $criteria, Context $context, array $associations = []): ?OrderEntity
{
foreach ($associations as $association) {
$criteria->addAssociation($association);
}
return $this->orderRepository->search($criteria, $context)->first();
}
public function getOrderByOrderNumber(string $orderNumber, Context $context, array $associations = []): ?OrderEntity
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('orderNumber', $orderNumber));
return $this->getOrderByCriteria($criteria, $context, $associations);
}
public function update(string $orderId, array $data, Context $context)
{
$data['id'] = $orderId;
$this->orderRepository->update([$data], $context);
}
}
src/Resources/config/services.xml
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Swag\PluginName\Storefront\Controller\SomethingController" public="true">
<argument type="service" id="Swag\PluginName\Service\OrderService"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
</services>
</container>
CodePudding user response:
It seems you're not using autowiring for your services, at least looking at your services.xml
.
Therefore you're missing the service registration of your OrderService
, so that you can use it as an argument for your controller.
This would in your case look like something like this:
<service id="Swag\PluginName\Service\OrderService">
<argument type="service" id="order.repository"/>
<argument type="service" id="logger"/>
</service>
You can read more about manually registering services in the Symfony documentation here