我通过Facebook进行了授权和身份验证,如下所示:http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html它有效
现在我想制作自己的事件,当用户使用 facebook 进行身份验证时,此事件将执行某些操作。例如,将用户重定向到主页。我是这样做的http://symfony.com/doc/current/components/event_dispatcher/introduction.html
所以我有这个课http://pastebin.com/2FTndtL4
我不知道如何实现它,我应该将什么作为参数传递给构造函数
这真的很简单。Symfony 2事件系统功能强大,服务标签可以完成这项工作。
- 将调度程序注入到要触发事件的类中。服务 ID 为
event_dispatcher
; - 在需要时用
$this->dispatcher->dispatch('facebook.post_auth', new FilterFacebookEvent($args))
触发事件; - 创建一个实现
EventSubscriberInterface
的服务,定义一个静态getSubscribedEvents()
方法。当然,您想收听facebook.post_auth
活动。
因此,您的静态方法将如下所示:
static public function getSubscribedEvents()
{
return array(
'facebook.post_auth' => 'onPostAuthentication'
);
}
public function onPostAuthentication(FilterFacebookEvent $event)
{
// Do something, get the event args, etc
}
最后将此服务注册为调度程序的订阅者:给它一个标签(例如。 facebook.event_subscriber
),然后进行RegisterFacebookEventsSubscribersPass
(请参阅本教程)。编译器传递应检索所有标记的服务,并在循环内调用:
$dispatcher = $container->getDefinition('event_dispatcher');
$subscribers = $container->findTaggedServiceIds('facebook.event_subscriber');
foreach($subscribers as $id => $attributes) {
$definition->addMethodCall('addSubscriber', array(new Reference($id)));
}
通过这种方式,您可以快速使订阅者(例如用于日志记录)简单地标记您的服务。
事件对象只是某种状态/数据存储。它保留可用于通过订阅者和/或侦听器调度某种事件的数据。因此,例如,如果您想将Facebook ID传递给您的听众 - 事件是存储它的正确方式。此外,事件是调度程序的返回值。如果要从侦听器/订阅服务器返回一些数据 - 也可以将其存储在事件对象中。