我试图在shopware 6中创建一个自定义API端点,以根据订单ID获取订单详细信息,但当我向控制器添加一些服务时,它显示不存在的服务,即使我添加了作为service.xml中的参数传递给构造函数的服务。
出现此错误的原因是什么?
我的文件;
源代码/店面/控制器/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;
}
}
源代码/服务/订单服务.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);
}
}
源代码/资源/配置/服务.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>
1条答案
按热度按时间gr8qqesn1#
看起来你没有为你的服务使用autowiring,至少看一下你的
services.xml
。因此你丢失了你的OrderService
的服务注册,这样你就可以把它作为你的控制器的参数。在您的情况下,这看起来像这样:
您可以在此处的Symfony文档中阅读有关手动注册服务的详细信息