在symfony 2中实现用户活动

时间:2022-02-11 06:48:10

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

在symfony 2中实现用户活动的最佳方法是什么?

And how to do it ?

怎么做?

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

我知道有一个symfony 2的事件系统。也许我应该触发一个事件?

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

更新每个页面请求还是有其他(更好)方法来更新用户活动是明智的吗?

2 个解决方案

#1


15  

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

跟踪用户请求(可能还有他们的活动)的好方法是监听kernel.request事件:

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官方文档中获得有关此主题的更多信息:

#2


-1  

public function onKernelController(FilterControllerEvent $event)
{

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

  }
}

#1


15  

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

跟踪用户请求(可能还有他们的活动)的好方法是监听kernel.request事件:

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官方文档中获得有关此主题的更多信息:

#2


-1  

public function onKernelController(FilterControllerEvent $event)
{

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

  }
}