Symfony Messenger与API平台集成:API平台未调度消息

问题描述

我尝试使用Symfony Messenger集成,并基于https://api-platform.com/docs/core/messenger/创建了一个非常简单的(同步)示例。

API端点正确返回了202响应代码,但不幸的是,API平台并未调度该消息– Xdebug既不在\Symfony\Component\Messenger\RoutableMessageBus::dispatch也不在\Symfony\Component\Messenger\TraceableMessageBus::dispatch的断点处中断。>

更多详细信息:消息处理程序中的dump('foo')不会被调用,并且从消息处理程序中记录日志消息也不会导致日志条目。

任何建议可能导致此问题的原因是什么

注意:Symfony Messenger在我的项目中总体上似乎正常运行-我可以成功发送消息,例如在控制器动作中。

我将Symfony 4.4.11和API Platform 2.5.6与PHP 7.2结合使用。

这是消息处理程序:

<?PHP
declare(strict_types=1);
namespace App\Api\MessageHandler;

use App\Entity\RPC\MarkNotificationAsRead;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class MarkNotificationAsReadMessageHandler implements MessageHandlerInterface
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function __invoke(MarkNotificationAsRead $markNotificationAsRead)
    {
        dump($markNotificationAsRead);
        $this->logger->info('Lorem ipsum!');
    }
}

这是代表API资源的类的代码

<?PHP
declare(strict_types=1);
namespace App\Entity\RPC;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ApiResource(
 *     messenger=true,*     collectio@R_502_6457@erations={
 *         "post"={"status"=202}
 *     },*     itemOperations={},*     output=false
 * )
 */
final class MarkNotificationAsRead
{
    /**
     * @var string
     * @Assert\NotBlank
     */
    public $foo;
}

注意:该类当前位于Entity命名空间中,但我没有为其添加任何Doctrine配置。

解决方法

我刚刚解决了我的问题:我必须启用Messenger for API平台。使用bin/console debug:config api_platform,我意识到默认情况下它是禁用的:

# config.yml
api_platform:
    messenger:
        enabled: true