0

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). 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:

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. But nothing logs. What should i do for it works?

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Slava 111
  • 37
  • 1
  • 4

1 Answers1

0

Did you try this ?

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();
    }
}
Ihor Kostrov
  • 2,361
  • 14
  • 23