Symfony2 Event Listener

So, I'm trying to figure out these listeners, but I'm having trouble finding any information on the symfony site regarding them.

At first, I wanted to create a listener that would be launched each time the page was loaded ... I thought that this could harm the overall performance of the system, so I thought that it only runs on: / and / otherpage

But then again, I am having trouble finding any information on where to start working with the listener. Any help is appreciated. All this, the listener will do, uses Doctrine to check the database and configure the session based on what it finds.

Again, any help or suggestions appreciated. Thank.

+5
source share
1 answer

- , , . :

services:
    page_load_listener:
        class: Acme\SecurityBundle\Controller\PageLoadListener
        arguments: 
            security: "@security.context", 
            container: "@service_container"
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 64 }

, , , , . , .

, .

namespace Acme\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class PageLoadListener extends controller
{
    private $securityContext;
    protected $container;
    protected $query;

    public function __construct(SecurityContext $context, $container, array $query = array())
    {
        $this->securityContext = $context;
        $this->container = $container;
        $this->query = $query;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {       
        //if you are passing through any data
        $request = $event->getRequest();

        //if you need to update the session data
        $session = $request->getSession();              

        //Whatever else you need to do...

    }
}

, , , , .

, !

+10

All Articles