Is it possible to use EventSubscribers with Silex?

狂风中的少年 提交于 2019-12-06 09:39:12

Your subscriber class should implement EventSubscriberInterface. If it does, just use the addSubscriber method instead of addListener.

$app['dispatcher']->addSubscriber(new MyEventSubscriber());

Your event subscriber has a method called getSubscribedEvents that tells Symfony what events to subscribe to so you don't need to pass an event name at all. See the Symfony docs for more information.

Update (security listener):

The basic HTTP authentication method is not considered interactive login. That is for a typical web based login form.

You may be able to use AuthenticationEvents::AUTHENTICATION_SUCCESS instead. Your listener method will receive an AuthenticationEvent instance.

<?php
namespace MySubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;

class ApiKeySubscriber implements EventSubscriberInterface
{
  public function onAuthenticate(AuthenticationEvent $event)
  {
    file_put_contents("/tmp/onInteractiveLogin.txt", "It was here");
  }

  public static function getSubscribedEvents()
  {
    return array(AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticate');
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!