13

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 ?

2
  • 2
    What do you mean by user activity? Aug 30, 2011 at 19:25
  • Well I've got a site with users and there browsing activity on the website. Active means that they are surfing between pages.
    – Vince V.
    Aug 30, 2011 at 19:45

2 Answers 2

16

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:

1
  • Thanks for your reply, I think I've got enough information now to build it!!
    – Vince V.
    Aug 30, 2011 at 20:36
-1
public function onKernelController(FilterControllerEvent $event)
{

    $controller = $event->getController();
    if ($controller[0] instanceof Controller  and (!strpos( get_class($controller[0]), 'App\FrontendBundle')===false) ) {

  }
}
1
  • 6
    Code only answers are frowned upon. Please explain what you've done and why this is "the best way".
    – RubberDuck
    Nov 12, 2014 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.