1

カスタム イベント ディスパッチャーとサブスクライバーの作成に取り組むのはこれが初めてなので、頭を包み込もうとしていますが、カスタム イベントがディスパッチされない理由がわかりません。

私はドキュメントに従っています。私の場合、誰かがサイトに登録したらすぐにイベントをディスパッチする必要があります。

だから私の中で私registerAction()はこのようなイベントをディスパッチしようとしています

$dispatcher = new EventDispatcher();
$event = new RegistrationEvent($user);
$dispatcher->dispatch(RegistrationEvent::NAME, $event);

これは私のRegistrationEventクラスです

namespace AppBundle\Event;
use AppBundle\Entity\User;
use Symfony\Component\EventDispatcher\Event;

class RegistrationEvent extends Event
{
    const NAME = 'registration.complete';

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function getUser(){
        return $this->user;
    }

}

これは私のRegistrationSubscriberクラスです

namespace AppBundle\Event;    
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class RegistrationSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::RESPONSE => array(
                array('onKernelResponsePre', 10),
                array('onKernelResponsePost', -10),
            ),
            RegistrationEvent::NAME => 'onRegistration'
        );

    }
    public function onKernelResponsePre(FilterResponseEvent $event)
    {
        // ...
    }

    public function onKernelResponsePost(FilterResponseEvent $event)
    {
        // ...
    }
    public function onRegistration(RegistrationEvent $event){

        var_dump($event);
        die;

    }

}

これを行った後、登録プロセスが関数で停止することを望んでいましたonRegistrationが、そうはなりませんでした。次に、プロファイラーの [イベント] タブを確認しましたが、イベントがリストされていません。

ここで何が欠けていますか?正しい方向へのプッシュは本当に高く評価されます。

更新: カスタム イベントのサービスを登録する必要があると考えたので、内部に次のコードを追加しました。services.yml

app.successfull_registration_subscriber:
    class: AppBundle\Event\RegistrationSubscriber
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: kernel.event_subscriber}

プロファイラーの [イベント] タブ内に、カスタム イベントが一覧表示されていますが、それでもディスパッチされません。

4

2 に答える 2