Skip to content

Instantly share code, notes, and snippets.

@11k
Last active March 23, 2023 15:47
Show Gist options
  • Save 11k/636d54e7e28657e0856836c07d68c9f7 to your computer and use it in GitHub Desktop.
Save 11k/636d54e7e28657e0856836c07d68c9f7 to your computer and use it in GitHub Desktop.
Using IpTraceable Doctrine Extension with Symfony 5.4
// src/EventSubscriber/IpTraceSubscriber.php
<?php
namespace App\EventSubscriber;
use Gedmo\IpTraceable\IpTraceableListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class IpTraceSubscriber implements EventSubscriberInterface
{
public function __construct(
private IpTraceableListener $ipTraceableListener
)
{
}
public function onKernelRequest(RequestEvent $event)
{
if (!$event->isMainRequest()) {
return;
}
$ip = $event->getRequest()->getClientIp();
if (!empty($ip)) {
$this->ipTraceableListener->setIpValue($ip);
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}
# config/services.yaml
services:
# ...
Gedmo\IpTraceable\IpTraceableListener:
tags: ['doctrine.event_subscriber']
calls:
- setAnnotationReader: ['@annotation_reader']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment