symfony - 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
.
更多详细信息:消息处理程序中的Adump('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,
* collectionOperations={
* "post"={"status"=202}
* },
* itemOperations={},
* output=false
* )
*/
final class MarkNotificationAsRead
{
/**
* @var string
* @Assert\NotBlank
*/
public $foo;
}
注意:这个类目前放在Entity
命名空间中,但我没有为它添加任何 Doctrine 配置。
解决方案
我刚刚解决了我的问题:我必须为 API 平台启用 Messenger。使用bin/console debug:config api_platform
我意识到默认情况下它是禁用的:
# config.yml
api_platform:
messenger:
enabled: true
推荐阅读
- matlab - 如何使 y 轴上的 MATLAB 图形中的公式与 LaTeX 解释器一起加粗?
- sharepoint - Sharepoint List 随机复制提交的项目
- c++ - 函数返回的值与 return 语句之前的函数体中的值不同
- r - 在 R 中更改合并地图的颜色
- java - Spring boot 非控制器异常处理——集中异常处理
- python - 如何使用正则表达式匹配文件中其他小写单词非连续重复的大写单词
- linux - 如何在 Linux 上优化这个定制的 Copy-on-Write POD 阵列?
- ios - 集合视图高度
- azure-blockchain-workbench - 删除和更新智能合约
- amazon-ec2 - 如何在 Ubuntu 18.04 TLS 上安装 AWS CloudHSM?(需要 libjson-c2 但只有 libjson-c3 可用)