I created a subscriber which executes another class->method, which creates a database entry. I can see that the subscriber fires twice, because the database entries are always entered double. I deactivated the Profiler (the Web Debug Toolbar) and that fixed the issue.
Here my Subscriber:
<?php
namespace App\EventSubscriber;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use App\Repository\UserRepository;
class ExampleSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents() {
return [
KernelEvents::RESPONSE => [
['onKernelResponsePre', 10],
['onKernelResponsePost', -10],
],
RequestEvent::class => 'onKernelRequest',
];
}
public function __construct(
LoggerInterface $logger,
UserRepository $user
) {
$this->logger = $logger;
$this->user = $user;
}
public function onKernelResponsePre(ResponseEvent $event)
{
// ...
}
public function onKernelResponsePost(ResponseEvent $event)
{
// ...
}
public function onKernelRequest(RequestEvent $event)
{
$db_data = $this->user->findUserByUsername($user_name);
$write_db_entry = $this->user->writeUserData($db_data['user_id']);
$this->logger->info('Just checking if this message shows twice. It does not.');
}
}
How can I get the code in "onKernelRequest" to just execute once and have the Profiler active? It could be in any event, but should execute before the controller gets called.
Thank you in advance for your help.