开发者

Implementing user activity in symfony 2

开发者 https://www.devze.com 2023-04-01 15:08 出处:网络
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 ?

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开发者_StackOverflow is it wise to update on every page request or are there other (better) ways to update user activity ?


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


public function onKernelController(FilterControllerEvent $event)
{

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

  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消