2016-07-04 7 views
0

こんにちは私は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 
* @[email protected] 
* @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> 

方法は、任意のコントローラのアクションのcorectly非常に初めを呼び出しているが、それはプロジェクト全体のすべてのコントローラを要求している、代わりに、私はそれだけで私のアプリケーションでは私の特定のモジュールに呼びたいです。

これに欠けているものをご案内ください。それは、kernel.controllerイベントリスナーがすべてのコントローラのために呼ばれていることを非常に正常だ事前

+0

あなたのコードのチェックは、あなたのコントローラがFinacial年のインスタンスではない場合、それは反対すべきではありませんか? 'if($ controller [0] instanceof FinancialYearController){' – goto

+0

私はそれを試してみましたが、うまくいきませんでした。 –

+0

だから私は自分のモジュールのルートを修正して、上記の機能でそれをチェックしようと思っています。 –

答えて

2

に おかげで、それはそれは、コントローラが一致しない場合は早期復帰のためにあなたを可能にする事項やイベントリスナーの内部チェックです。

あなたのチェックは間違っているようです。おそらく、コントローラは、あなたが期待したクラスでない場合、戻りたい:

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

}

+0

このコードで何が問題になっていますか? –

+0

どのコードに問題がありますか?私が掲示したコードの上の – Gerry

+0

。あなたが間違ったチェックがあると言ったように –

関連する問題