如何调用内核终止事件

问题描述 投票:0回答:1

我正在尝试将响应发送给用户,并使其在我的服务中变得很难。我在那里找到了另一个线程(How tout use kernel.terminate Event in a Service)。但它不起作用。当我尝试在其中进行操作时,我的日志中收到下一条消息:php.INFO:不建议使用的用户:调用“ Symfony \ Component \ EventDispatcher \ EventDispatcherInterface :: dispatch()”所以我就这样尝试了:

public function makeHardWork()
{
    $this->dispatcher->addListener(KernelEvents::TERMINATE, function (Event $event){
        $this->logger->log(LogLevel::ERROR,'123456');
    },1);
}

[我也试图在$ index.php中转储$ kernel,并且有我的订阅者。但是没有日志。我应该怎么做才能起作用?

php symfony events service kernel
1个回答
0
投票

您尝试过这个吗?

services.yaml

App\EventListener\SomeEventListener:
    tags:
        - { 'name': 'kernel.event_listener', 'event': 'kernel.terminate', 'method': 'onTerminateEvent' }

SomeEventListener

<?php

namespace App\EventListener;

use Symfony\Component\HttpKernel\Event\TerminateEvent;

class SomeEventListener
{
    private $someSerive;

    public function __construct(SomeService $someService)
    {
        $this->someService = $someService;
    }

    public function onTerminateEvent(TerminateEvent $event)
    {
        $request = $event->getRequest();
        if ($request->get('_route') !== 'some_route_name') {
            return;
        }

        $this->someService->makeHardWork();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.