5

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

Initially, I wanted to create a listener that would trigger on every page load... I figured that could be detrimental to the overall system performance so i then thought of having it trigger only on: / and /otherpage

But again, i'm having issues finding any information on where to get started with the listener. Any help appreciated.. All this listener will be doing, is using Doctrine to check the database and setting a session based on what it finds..

Again, any help or suggestions appreciated. Thanks.

1 Answer 1

10

I do something similar to check the subdomain hasn't changed. You can put the listener in as a service in your config file as follows:

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 }

I'm not exactly sure how priority works but I've found if it's set too high it wouldn't run before the rest of the application. It's on my to-do list to research a little more.

Here is an example of how the listener could look.

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

    }
}

I'm not sure of the best way to set it to only run on certain pages, but my best guess would be to check the route and only hit your db it when the route matches whatever you set.

Hope that gets you started!

Greg

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the info Greg! I'll have a play around and see what I can come up with off this... I might even be able to get by with having it check on every page load (all the queries will do is check the DB to see if there is an entry for today for the user or not).. Thanks again!
No problem. I wouldn't worry too much about it calling the listener function on each page load, it's such a tiny service that it shouldn't have any affect on performance, just try to work out a way to avoid the listener hitting the DB every time unless it specifically needs to. Let me know how you go, happy to try and help if you have any problems.
I'm doing the same thing, but if nothing is found in the Listener query I want to short circuit everything and redirect to a "Coming Soon" template page. Any thoughts on how to construct that within the Listener? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.