簡體   English   中英

在每個模塊上調用事件監聽器Symfony2

[英]Event Listener Called on Every Module Symfony2

嗨,我在symfony2中有一個事件監聽器,我也進行了相應的注冊,它需要在模塊中任何控制器的任何函數調用之前調用。 但這是在調用整個應用程序,我的意思是每個模塊。 但是我只希望有人打開我的模塊時才調用它。

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

現在,當Method完全調用任何控制器的任何操作的核心開始時,但是它正在調用整個項目的每個控制器,相反,我希望它僅在應用程序的“我的特定模塊”中調用。

請指導此中缺少的內容。 提前致謝

每個控制器調用kernel.controller事件監聽器是很正常的,重要的是事件監聽器內部的檢查,如果控制器不匹配,則允許您盡早返回。

不過,您的支票確實有誤。 如果控制器不是您期望的類,則可能要返回:

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