Symfony Tactician bundle TypeHists=缺少处理程序方法错误
我以前在Symfony中使用过TheppLeague/tactician捆绑包,但这是我第一次在Symfony 4.*(特别是4.1.4)中使用它,并尝试为我的应用程序服务使用单个处理程序类 当我在控制器中执行命令时Symfony Tactician bundle TypeHists=缺少处理程序方法错误,symfony,typehints,Symfony,Typehints,我以前在Symfony中使用过TheppLeague/tactician捆绑包,但这是我第一次在Symfony 4.*(特别是4.1.4)中使用它,并尝试为我的应用程序服务使用单个处理程序类 当我在控制器中执行命令时 public function postAction(Request $request, CommandBus $commandBus) { $form = $this->createForm(VenueType::class); $form->subm
public function postAction(Request $request, CommandBus $commandBus)
{
$form = $this->createForm(VenueType::class);
$form->submit($request->request->all(), true);
$data = $form->getData();
if($form->isValid()) {
$command = new CreateVenueCommand($data);
$commandBus->handle($command);
return $form->getData();
}
return $form;
}
。。。我得到以下错误:
"error": {
"code": 500,
"message": "Internal Server Error",
"exception": [
{
"message": "Could not invoke handler for command App\\Application\\Command\\CreateVenueCommand for reason: Method 'handle' does not exist on handler",
"class": "League\\Tactician\\Exception\\CanNotInvokeHandlerException",
"trace": [
我似乎遵循了tactician捆绑包的设计,并使用Flex安装了它。就我所知,所有配置都是正确的,所以我不确定我的实现中缺少了什么
实施
根据《HPLeague/tactician捆绑包安装指南》,我已经使用Flex安装了该捆绑包,该捆绑包已注册,配置包已安装:
tactician:
commandbus:
default:
middleware:
- tactician.middleware.locking
- tactician.middleware.doctrine
- tactician.middleware.command_handler
创建DTO命令类“CreateVenueCommand”后,我创建了处理程序类:
use App\Infrastructure\Domain\Model\VenueRepositoryInterface;
use App\Application\Command\CreateVenueCommand;
use App\Domain\Entity\Venue;
class VenueApplicationService
{
private $venueRepository;
public function __construct(VenueRepositoryInterface $venueRepository)
{
$this->venueRepository = $venueRepository;
}
/**
* @param CreateVenueCommand $aCommand
* @throws \Exception
*/
public function createVenue(CreateVenueCommand $aCommand)
{
$aVenue = new Venue($aCommand->getData())
if ($aVenue === null) {
throw new \LogicException('Venue not created');
}
$this->venueRepository->add($aVenue);
}
然后,我利用Symfony的自动布线和战术家的TypeHits将handler类注册为服务:
App\Application\VenueApplicationService:
arguments:
- '@App\Infrastructure\Persistence\Doctrine\DoctrineVenueRepository'
tags:
- { name: tactician.handler, typehints: true }
因此,根据公式,如果:
public function handle(CreateVenueCommand $aCommand)
{
。。。那么它工作得很好。这似乎表明类型提示没有按照文档所述的方式工作
在这种情况下,方法的实际名称似乎很重要。。。。或者我在我的实现中犯了某种形式的错误。。。或者我误解了多个命令进入一个处理程序用例
如蒙协助,将不胜感激
解决方案
非常感谢kunicmarko20为我指明了正确的方向
特别是在我的用例中,我只需要使用Tacticians的MethodNameInflector类之一,在Symfony中配置,因此:
tactician:
commandbus:
default:
middleware:
- tactician.middleware.locking
- tactician.middleware.doctrine
- tactician.middleware.command_handler
method_inflector: tactician.handler.method_name_inflector.handle_class_name
。。。然后,只需在我的应用程序服务类'handle{whateverYouLike}命令下命名每个处理程序方法。说明了命名的工作原理,如果要使用与此表中不同的名称,可以实现MethodNameInflector接口并提供方法的名称