douduxia1551 2014-09-08 20:43
浏览 25
已采纳

Symfony 2和FOSUserBundle - 在注册期间从用户路径添加信息

I have a Symfony 2 application making use of FOSUserBundle. The app will be used by members of a number of different organisations, and I'd like them to be able to go to an organisation-specific URL in order to register, for example http://www.myapp.com/[organisation_name]/register. How can I let FOSUserBundle handle the registration, but also add [organisation_name] to the User record it creates?

My research suggests I might need to hook into a FOSUserBundle event (REGISTRATION_SUCCESS?), but I'm not sure about the exact mechanics of that or getting info from the URL.

FOSUserBundle Events

  • 写回答

1条回答 默认 最新

  • dongliao4353 2014-09-09 05:52
    关注

    You can use GET attribute, like this:

    http://www.myapp.com/register?org=acme
    

    Event subscriber, in this case, will look like this:

    namespace Acme\DemoBundle\Event;
    
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use FOS\UserBundle\Event\FormEvent;
    
    class EventSubscriber implements EventSubscriberInterface
    {
        public static function getSubscribedEvents()
        {
            return array(
                'fos_user.registration.success'  => array('onRegistrationSuccess', 10),
            );
        }
    
        public function onRegistrationSuccess(FormEvent $event)
        {
            $user = $event->getUser();
            $org = $event->getRequest()->query->get('org');
            $user->setOrganisation($org);
        }
    }
    

    And don't forget to register your subscriber in services.yml:

    acme_demo.event_subscriber:
        class: Acme\DemoBundle\Event\EventSubscriber
        tags:
            - { name: kernel.event_subscriber }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题