Last active
March 23, 2023 15:47
-
-
Save 11k/636d54e7e28657e0856836c07d68c9f7 to your computer and use it in GitHub Desktop.
Using IpTraceable Doctrine Extension with Symfony 5.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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', | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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