哪里调用events.php,PHP-Symfony事件订阅者不响应调度

我正在使用带有自动接线调度程序的服务来调度事件.为了测试事件,我在调度事件之前添加了一个事件订阅者.但是,注册订户没有记录我希望它执行的方法.

事件:

namespace App\Event;

use Symfony\Component\EventDispatcher\Event;

class GameStartEvent extends Event {

}

订户:

namespace App\Event;

use Psr\Log\LoggerAwareTrait;

use Psr\Log\LoggerInterface;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class GameStartSubscriber implements EventSubscriberInterface {

use LoggerAwareTrait;

public function __construct(LoggerInterface $logger) {

$this->setLogger($logger);

}

public static function getSubscribedEvents() {

return [

"game.started" => [

[

'logEvent',

],

]

];

}

public function logEvent(GameStartEvent $event): void {

$this->logger->info("the game has started");

}

}

这是使用过的服务,用于分派事件,而事件注册应该在将来的其他地方发生.我只是在这里做测试:

namespace App\Service;

use App\Event\GameStartEvent;

use App\Event\GameStartSubscriber;

use Psr\Log\LoggerInterface;

use Psr\SimpleCache\CacheInterface;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class GameCacheService {

private $eventDispatcher;

private $logger;

public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) {

$this->eventDispatcher = $eventDispatcher;

$this->logger = $logger;

}

// some other methods...

function startGame() {

// some code...

$gameStartSubscriber = new GameStartSubscriber($this->logger);

$this->eventDispatcher->addSubscriber($gameStartSubscriber);

$this->eventDispatcher->dispatch("game.started", new GameStartEvent());

}

}

调用服务方法后,不会记录指定的记录器消息.

解决方法:

更换

$this-> eventDispatcher-> dispatch(“ game.started”,new GameStartEvent());`

$this-> eventDispatcher-> dispatch(“ game.started”,$event);`

您似乎在App / Entity中至少拥有您的事件,该事件默认情况下不会自动关联,因此不应该自动关联.看一下您的services.yml,应该将其排除在自动装配之外.将您的东西移动到类似App / Event的地方

标签:symfony,listener,events,php

来源: https://codeday.me/bug/20191211/2106661.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值