こんにちは私は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
イベントリスナーがすべてのコントローラのために呼ばれていることを非常に正常だ事前
あなたのコードのチェックは、あなたのコントローラがFinacial年のインスタンスではない場合、それは反対すべきではありませんか? 'if($ controller [0] instanceof FinancialYearController){' – goto
私はそれを試してみましたが、うまくいきませんでした。 –
だから私は自分のモジュールのルートを修正して、上記の機能でそれをチェックしようと思っています。 –