Symfony 不接受 API 的处理程序
Symfony doesn't take me the handler of an API
我正在一个名为 Sylius 的框架中做一个 API,该框架使用 symfony 框架作为基础。
我目前无法被解决API逻辑
的API控制器class识别
我有一个控制器及其各自的声明:
<?php
declare(strict_types=1);
namespace EasyCommerce\ShopApiPlugin\Controller\Order;
use EasyCommerce\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use EasyCommerce\ShopApiPlugin\Request\Order\CustomerOrderCancellationRequest;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final class CustomerOrderCancellationAction
{
/** @var ViewHandlerInterface */
private $viewHandler;
/** @var MessageBusInterface */
private $bus;
/** @var ValidatorInterface */
private $validator;
/** @var ValidationErrorViewFactoryInterface */
private $validationErrorViewFactory;
public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
ValidatorInterface $validator,
ValidationErrorViewFactoryInterface $validationErrorViewFactory
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->validator = $validator;
$this->validationErrorViewFactory = $validationErrorViewFactory;
}
public function __invoke(Request $request): Response
{
$pickupRequest = new CustomerOrderCancellationRequest($request);
$validationResults = $this->validator->validate($pickupRequest);
if (0 === count($validationResults)) {
$this->bus->dispatch($pickupRequest->getCommand());
return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
}
}
控制器声明如下:
<service id="sylius.shop_api_plugin.controller.order.customer_order_cancellation_action"
class="EasyCommerce\ShopApiPlugin\Controller\Order\CustomerOrderCancellationAction"
>
<argument type="service" id="fos_rest.view_handler" />
<argument type="service" id="sylius_shop_api_plugin.command_bus" />
<argument type="service" id="validator" />
<argument type="service" id="sylius.shop_api_plugin.factory.validation_error_view_factory" />
</service>
我是这样声明路由的:
sylius_shop_api_customer_order_cancellation:
path: /orders/{tokenValue}/cancellation
methods: [PUT]
defaults:
_controller: sylius.shop_api_plugin.controller.order.customer_order_cancellation_action
它 returns 出现以下错误:
{
"code": 500,
"message": "Attempted to load class \"CustomerOrderCancellationAction\" from namespace \"EasyCommerce\ShopApiPlugin\Controller\Order\".\nDid you forget a \"use\" statement for another namespace?"
}
为了测试路由是否配置正确,我将它指向另一个控制器并且它可以工作。问题出在您创建的控制器上。
由于主项目中有两个不同的命名空间,我不得不清除缓存:
bin/console cache:clear --env=dev
然后重新加载作曲家:
composer dump-autoload
这样它会重新加载所有命名空间
我正在一个名为 Sylius 的框架中做一个 API,该框架使用 symfony 框架作为基础。 我目前无法被解决API逻辑
的API控制器class识别我有一个控制器及其各自的声明:
<?php
declare(strict_types=1);
namespace EasyCommerce\ShopApiPlugin\Controller\Order;
use EasyCommerce\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
use EasyCommerce\ShopApiPlugin\Request\Order\CustomerOrderCancellationRequest;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final class CustomerOrderCancellationAction
{
/** @var ViewHandlerInterface */
private $viewHandler;
/** @var MessageBusInterface */
private $bus;
/** @var ValidatorInterface */
private $validator;
/** @var ValidationErrorViewFactoryInterface */
private $validationErrorViewFactory;
public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
ValidatorInterface $validator,
ValidationErrorViewFactoryInterface $validationErrorViewFactory
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->validator = $validator;
$this->validationErrorViewFactory = $validationErrorViewFactory;
}
public function __invoke(Request $request): Response
{
$pickupRequest = new CustomerOrderCancellationRequest($request);
$validationResults = $this->validator->validate($pickupRequest);
if (0 === count($validationResults)) {
$this->bus->dispatch($pickupRequest->getCommand());
return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
}
return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
}
}
控制器声明如下:
<service id="sylius.shop_api_plugin.controller.order.customer_order_cancellation_action"
class="EasyCommerce\ShopApiPlugin\Controller\Order\CustomerOrderCancellationAction"
>
<argument type="service" id="fos_rest.view_handler" />
<argument type="service" id="sylius_shop_api_plugin.command_bus" />
<argument type="service" id="validator" />
<argument type="service" id="sylius.shop_api_plugin.factory.validation_error_view_factory" />
</service>
我是这样声明路由的:
sylius_shop_api_customer_order_cancellation:
path: /orders/{tokenValue}/cancellation
methods: [PUT]
defaults:
_controller: sylius.shop_api_plugin.controller.order.customer_order_cancellation_action
它 returns 出现以下错误:
{
"code": 500,
"message": "Attempted to load class \"CustomerOrderCancellationAction\" from namespace \"EasyCommerce\ShopApiPlugin\Controller\Order\".\nDid you forget a \"use\" statement for another namespace?"
}
为了测试路由是否配置正确,我将它指向另一个控制器并且它可以工作。问题出在您创建的控制器上。
由于主项目中有两个不同的命名空间,我不得不清除缓存:
bin/console cache:clear --env=dev
然后重新加载作曲家:
composer dump-autoload
这样它会重新加载所有命名空间