跟踪用户请求(也可能是他们的活动)的好方法是收听kernel.request事件:
听众类:
namespace Acme\YourBundle\EventListener; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\DependencyInjection\ContainerInterface; class RequestListener { /** * Container * * @var ContainerInterface */ protected $container; /** * Listener constructor * * @param ContainerInterface $container */ public function __construct(ContainerInterface $container) { $this->container = $container; } /** * kernel.request Event * * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); // Here you can intercept all HTTP requests,and through $container get access to user information } }
监听器的配置:
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <parameters> <parameter key="acme.request_listener.class">Acme\YourBundle\EventListener\RequestListener</parameter> </parameters> <services> <service id="acme.request_listener" class="%acme.request_listener.class%"> <tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" /> <argument type="service" id="service_container" /> </service> </services> </container>