简体   繁体   English

如何调用 kernel 终止事件

[英]how to call kernel terminate event

Im trying to send response to user and after it make hard work in my service.我试图向用户发送响应,并在它为我的服务做出努力之后。 I found another thread there( How tout use kernel.terminate Event in a Service ).我在那里找到了另一个线程( 如何在服务中使用 kernel.terminate 事件)。 But it doesnt works.When i tried make like in there i got next messages in my logs: php.INFO: User Deprecated: Calling the "Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()" So then i tried it like that:但它不起作用。当我尝试在那里制作时,我在日志中收到下一条消息: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);
}

Also i tried to dump $kernel in index.php and there is my subscriber.我还尝试将 $kernel 转储到 index.php 中,并且有我的订阅者。 But nothing logs.但没有任何记录。 What should i do for it works?我应该怎么做才能工作?

Did you try this?你试过这个吗?

services.yaml服务.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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM