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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器