简体   繁体   中英

Event Listener Called on Every Module Symfony2

Hi I have a event Listener in symfony2, That I have registered accordingly as well, it need to call at before of any function call of any controller in my module. But it is calling on Whole application, I mean every module. but I want it to called only when some one will open My Module only.

//My Event Listener
namespace Edu\AccountBundle\EventListener;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Edu\AccountBundle\CommonFunctions\CommonFunctions;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Edu\AccountBundle\Controller\FinancialYearController;

/*
 * Class:BeforeControllerListener
 * @DESC:its a Listener which will execute at very first of any action     of any  controller in Account module (Act as a beforeFilter Symfony2)
 * @param : @session,@route,@db
 * @sunilrawat@indivar.com
 * @09-07-2015
 */

class BeforeControllerListener
{

private $session;
private $router;
private $commonFunctions;

public function __construct(Session $session, Router $router, DocumentManager $dm)
{
    $this->session = $session;
    $this->router = $router;
    $this->dm = $dm;
    $this->commonFunctions = new CommonFunctions();
}
public function onKernelController(FilterControllerEvent $event)
{
    $controller = $event->getController();
    if (!is_array($controller)) {
        return;
    }
    if (!$controller[0] instanceof FinancialYearController) {
        if ($this->commonFunctions->checkFinancialYear($this->dm) !== 0 ) {
            return;
        }
        $this->session->getFlashBag()->add('error', 'OOPS!, YOU MUST CREATE FINANCIAL YEAR TO MAKE ANY PROCESS IN ACCOUNTS!');
        $redirectUrl= $this->router->generate('financialyear');
        $event->setController(function() use ($redirectUrl) {
            return new RedirectResponse($redirectUrl);
        });
    }
}
}
//Services.xml
<service id="edu.account.listener" class="Edu\AccountBundle\EventListener\BeforeControllerListener">
        <argument type="service" id="session"/>
        <argument type="service" id="router"/>
        <argument type="service" id="doctrine_mongodb.odm.document_manager"/>
            <tag name="kernel.event_listener" event="kernel.controller" method="onKernelController"/>
    </service>

Now when the Method is calling corectly very begining of any action of any controller, but it is calling for every controller of whole project, Instead I want it to call only in My Particular Module in my application.

Please guide what is missing in this. Thanks in advance

It's quite normal that a kernel.controller event listener is called for every controller, it's the check inside the event listener that matters and that allows you for an early return if the controller does not match.

Your check does seem wrong though. You probably want to return if the controller is not the class you expect:

public function onKernelController(FilterControllerEvent $event)
{
    $controller = $event->getController();

    if (!is_array($controller)) {
        return;
    }

    if (!$controller[0] instanceof FinancialYearController) {
        return;
    }

    if ($this->commonFunctions->checkFinancialYear($this->dm) !== 0 ) {
        return;
    }

    $this->session->getFlashBag()->add('error', 'OOPS!, YOU MUST CREATE FINANCIAL YEAR TO MAKE ANY PROCESS IN ACCOUNTS!');
    $redirectUrl= $this->router->generate('financialyear');
    $event->setController(function() use ($redirectUrl) {
        return new RedirectResponse($redirectUrl);
    });
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM