Symfony Messenger 与 API 平台集成:API 平台不发送消息
Symfony Messenger Integration with API Platform: message does not get dispatched by API Platform
我尝试使用 Symfony Messenger 集成并基于 https://api-platform.com/docs/core/messenger/ 创建了一个非常简单的(同步)示例。
API 端点正确 returns 一个 202
响应代码,但不幸的是,消息没有被 API 平台派发 – Xdebug 既不会在断点处中断 \Symfony\Component\Messenger\RoutableMessageBus::dispatch
也不在 \Symfony\Component\Messenger\TraceableMessageBus::dispatch
.
更多详细信息:消息处理程序中的 dump('foo')
不会被调用,并且从消息处理程序中记录日志消息也不会产生日志条目.
有什么建议是这个问题的原因吗?
注意: Symfony Messenger 在我的项目中似乎总体上运行良好——我可以成功发送一条消息,例如在控制器操作中。
我使用 Symfony 4.4.11 和 API 平台 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 资源的 class 的代码:
<?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;
}
注意:这个class目前放在Entity
命名空间中,但我没有为它添加任何Doctrine配置。
我刚刚解决了我的问题:我必须为 API 平台启用 Messenger。使用 bin/console debug:config api_platform
我意识到默认情况下它是禁用的:
# config.yml
api_platform:
messenger:
enabled: true
我尝试使用 Symfony Messenger 集成并基于 https://api-platform.com/docs/core/messenger/ 创建了一个非常简单的(同步)示例。
API 端点正确 returns 一个 202
响应代码,但不幸的是,消息没有被 API 平台派发 – Xdebug 既不会在断点处中断 \Symfony\Component\Messenger\RoutableMessageBus::dispatch
也不在 \Symfony\Component\Messenger\TraceableMessageBus::dispatch
.
更多详细信息:消息处理程序中的 dump('foo')
不会被调用,并且从消息处理程序中记录日志消息也不会产生日志条目.
有什么建议是这个问题的原因吗?
注意: Symfony Messenger 在我的项目中似乎总体上运行良好——我可以成功发送一条消息,例如在控制器操作中。
我使用 Symfony 4.4.11 和 API 平台 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 资源的 class 的代码:
<?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;
}
注意:这个class目前放在Entity
命名空间中,但我没有为它添加任何Doctrine配置。
我刚刚解决了我的问题:我必须为 API 平台启用 Messenger。使用 bin/console debug:config api_platform
我意识到默认情况下它是禁用的:
# config.yml
api_platform:
messenger:
enabled: true