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

时间:2016-07-04 04:18:05

标签: php symfony

嗨我在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调用corectly开始任何控制器的任何操作时,它会调用整个项目的每个控制器,而不是我希望它只在我的应用程序中的My Particular Module中调用。

请指导中缺少的内容。 提前致谢

1 个答案:

答案 0 :(得分:2)

每个控制器调用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);
    });
}

}

相关问题