2014-05-03 39 views
0

我在我的Symfony 2.3項目中使用FOSUserBundle,並且我想在用戶確認他的賬戶點擊郵件鏈接後立即執行一些代碼。我試圖重寫在用戶確認註冊後立即執行一個動作

class RegistrationController extends ContainerAware 
{ 
... 
} 

,但沒有運氣:(

我可以創建任何類型的事件的這個動作後立即執行?

謝謝!

+0

覆蓋[RegistrationController](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/RegistrationController.php)函數'confirmAction($ token)' – tttony

回答

0

您可以創建一個EventListener聽這樣的FOSUserEvents::REGISTRATION_CONFIRMED

use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use FOS\UserBundle\FOSUserEvents; 
use FOS\UserBundle\Event\UserEvent; 

class RegistrationConfirmedEventListener implements EventSubscriberInterface 
{ 
    public static function getSubscribedEvents() 
    { 
     return array(FOSUserEvents::REGISTRATION_CONFIRMED => 'performOnRegistrationConfirmed'); 
    } 

    public function performOnRegistrationConfirmed(UserEvent $event) 
    { 
     $user = $event->getUser(); 
     // Do something 
    } 
} 
+0

似乎performOnRegistrationConfirmed方法不是我執行的方式,我創建XXX/myBundle/Event /中的新文件RegistrationConfirmed.php,並將其粘貼到其中。我錯過了什麼嗎?對不起,我是Symfony 2的新手。謝謝。 – relez

+0

您必須[將事件偵聽器註冊爲服務](http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html#registering-the-listener)。編輯:我只是注意到這不是最好的例子,我現在找不到更好的一個。只需將你的類添加到你的'service.yml'中,並用'name:'kernel.event_subscriber'' – dbrumann

+0

來標記它就是一個更好的鏈接:http://symfony.com/doc/master/cookbook/doctrine/event_listeners_subscribers.html #配置-的偵聽器用戶 – dbrumann