我正在嘗試在 shopware 6 中創建一個自定義 api 端點,以根據訂單 ID 獲取訂單詳細資訊,但是當我向控制器添加一些服務時,即使我添加了傳遞給建構式的那些服務,它也會顯示不存在的服務作為 service.xml 中的引數。
出現此錯誤的原因是什么?
我的檔案;
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>
uj5u.com熱心網友回復:
似乎您沒有為您的服務使用自動裝配,至少查看您的services.xml. 因此,您缺少 的服務注冊OrderService,因此您可以將其用作控制器的引數。
在您的情況下,這看起來像這樣:
<service id="Swag\PluginName\Service\OrderService">
<argument type="service" id="order.repository"/>
<argument type="service" id="logger"/>
</service>
您可以在此處的 Symfony 檔案中閱讀有關手動注冊服務的更多資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517986.html
上一篇:在BigQuery中計數直到條件
