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 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序