2017-10-07 2 views
0

私は例 https://symfony.com/doc/current/event_dispatcher.html#content_wrappersymfonyの3.3.9リスナーないcalles

のような単純なリスナーの例を持っている1:1

services.ymlは同じ

services: 
    # default configuration for services in *this* file 
    _defaults: 
     # automatically injects dependencies in your services 
     autowire: true 
     # automatically registers your services as commands, event subscribers, etc. 
     autoconfigure: true 
     # this means you cannot fetch services directly from the container via $container->get() 
     # if you need to do this, you can override this setting on individual services 
     public: false 

    # makes classes in src/AppBundle available to be used as services 
    # this creates a service per class whose id is the fully-qualified class name 
    # AppBundle\: 
    #  resource: '../../src/AppBundle/*' 
    #  # you can exclude directories or files 
    #  # but if a service is unused, it's removed anyway 
    #  exclude: '../../src/AppBundle/{Entity,Repository,Tests}' 
    AppBundle\: 
     resource: '../../src/AppBundle/*' 
     # you can exclude directories or files 
     # but if a service is unused, it's removed anyway 
     exclude: '../../src/AppBundle/{Entity,Repository}' 
    # controllers are imported separately to make sure they're public 
    # and have a tag that allows actions to type-hint services 
    # AppBundle\Controller\: 
    #  resource: '../../src/AppBundle/Controller' 
    #  public: true 
    #  tags: ['controller.service_arguments'] 
    AppBundle\Controller\: 
     resource: '../../src/AppBundle/Controller' 
     public: true 
     tags: ['controller.service_arguments'] 

ですがリスナーは「呼び出されていないリスナー」と表示されます

私は間違っていますか?それは与えられたインタフェースを実装する場合

+0

が優先問題になる可能性があります属性なしで自動設定は、タグのみで動作します。エラーメッセージは、リスナーが認識されて追加されたことを示します。私はあなたが正しいメソッド名を持っていると仮定します。したがって、優先度の高い別のリスナーが最初に呼び出されてからイベントをキャンセルする可能性があります。 bin/consoleデバッグ:イベントディスパッチャが役立つ可能性があります。 – Cerad

答えて

2

クラスは、自動タグ付けすることができる - これはEventSubscriber例の場合です。リスナーがコンテナビルダ(インターフェイスまたはそれが拡張するクラス)へのヒントを持たない場合、リスナーとしてタグ付けする必要があるかどうか、またはイベントを知る方法はありません。

例のように、構成内のリスナーに明示的にタグを付ける必要があります。

# app/config/services.yml 
services: 
    AppBundle\EventListener\ExceptionListener: 
     tags: 
      - { name: kernel.event_listener, event: kernel.exception } 

、イベント間の明示的なマッピングを有するように、加入者は、推測することができる - KernelEvents::EXCEPTIONように、getSubscribedEvents()及びクラスメソッドが実行されます。

これは、すべてのタグについてない作業を行いThe Symfony 3.3 DI Container Changes Explained

にそれが言うように。多くのタグには、イベントリスナーなどの必須属性があります。この場合、タグにイベント名とメソッドも指定する必要があります。任意の必要なタグが

+0

彼は3.3のため、タグは必要ありません。彼はAppBundle \ EventListener \ ExceptionListenerだけを実行できます。 タグ:... – kunicmarko20

+1

名前またはクラス名も同様に機能します。私は、速さと参照の明快さのために、ドキュメンテーションからコピーしました。 –

+0

正しく読むと、services.ymlを設定する必要はありません。 3.3では、EventListenerディレクトリ内のリスナーが自動的にロードされます。 – megadruck

関連する問題