简体   繁体   English

在每个模块上调用事件监听器Symfony2

[英]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. 嗨,我在symfony2中有一个事件监听器,我也进行了相应的注册,它需要在模块中任何控制器的任何函数调用之前调用。 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. 现在,当Method完全调用任何控制器的任何操作的核心开始时,但是它正在调用整个项目的每个控制器,相反,我希望它仅在应用程序的“我的特定模块”中调用。

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. 每个控制器调用kernel.controller事件监听器是很正常的,重要的是事件监听器内部的检查,如果控制器不匹配,则允许您尽早返回。

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);
    });
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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