Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing user activity in symfony 2

What's the best way to implement user activity in symfony 2 ?

And how to do it ?

I know there is the event system of symfony 2. Maybe I should trigger an event ?

And is it wise to update on every page request or are there other (better) ways to update user activity ?

like image 774
Vince V. Avatar asked Aug 30 '11 18:08

Vince V.


1 Answers

Good way to track user requests (and possibly their activity) is to listen to kernel.request event:

Listener class:

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
    }
}

Configuration for the listener:

<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>

You can get more info on this topic in the official Symfony documentation:

  • Symfony: Internal requests
  • Symfony: Service container
like image 123
dmirkitanov Avatar answered Sep 28 '22 14:09

dmirkitanov