douwan4993 2014-04-29 20:35
浏览 31
已采纳

FOS覆盖注册控制器

I'm trying to understand and use FOSUserbundle.

I have a simple logic to add to the registration process, link the user to a customer. For this I created a child bundle (FOS is correctly set up fo far).

Then I have the following code for the controller:

class RegistrationController extends BaseController
    {
        public function RegisterAdminUserAction(Request $request)
        {
            $em = $this->getDoctrine()->getManager();
            $customer = new Customer();
            $customer = $em->getRepository('MyRepo:Customer')->findOneById(1);



        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->container->get('fos_user.registration.form.factory');
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->container->get('fos_user.user_manager');
        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->container->get('event_dispatcher');

        $user = $userManager->createUser();
        $user->setEnabled(true);

        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

        if (null !== $event->getResponse())
        {
            return $event->getResponse();
        }

        $form = $formFactory->createForm();
        $form->setData($user);


        if ('POST' === $request->getMethod()) 
         {
            $form->bind($request);

            //form received
            if ($form->isValid()) 
            {
                $event = new FormEvent($form, $request);
                $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

                $user->setType("CustomerAdmin");
                $user->setCustomer($customer);
                $userManager->updateUser($user);


                if (null === $response = $event->getResponse()) 
                {
                    $url = $this->container->get('router')->generate('fos_user_registration_confirmed');
                    $response = new RedirectResponse($url);
                }

                $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

                return $response;
            }
        }
        else
        {    
            return $this->container->get('templating')->renderResponse('NRtworksSubscriptionBundle:RegisterAdmin:registration.html.'.$this->getEngine(), array(
        'RegisterAdminForm' => $form->createView(),
    ));

        }
    }
}

This is just the code I pasted from the original controller.

I have two questions:

  • my call to getDoctrine() does not work, obviously because the controller does not extends the master class Controller. My question is then how can I access the entity manager and all doctrine functions in a controller that extends the FOS one ?

  • as you can see the last part of the controller: $this->container->get('templating')->renderResponse('NRtworksSubscriptionBundle:RegisterAdmin:registration.html.'.$this->getEngine(), array( 'RegisterAdminForm' => $form->createView(), )); is different than the original. I had to put one of my own otherwise the form would redirect me toward the original FOS controller. Is that the "right" way to use FOS (chaning the path of the form in the twig rather than have it link in the PHP, seems dirty to me) ?

  • 写回答

2条回答 默认 最新

  • douyun3022 2014-04-29 20:51
    关注

    If your controller is extending the "base" registration controller from FOSUserBundle (e.g. FOS\UserBundle\Controller\RegistrationController) then you have a ContainerAware object, but as you point out, not the base Symfony controller which provides certain convenience methods.

    The base controller's getDoctrine() method simply is a convenience method to return the doctrine service from the container, e.g.

    public function getDoctrine()
    {
        if (!$this->container->has('doctrine')) {
            throw new \LogicException('The DoctrineBundle is not registered in your application.');
        }
    
        return $this->container->get('doctrine');
    }
    

    So to answer your questions:

    1. Either add a convenience method to your RegistrationController, similar to the above, or simply replace calls to $this->getDoctrine() with a call to return the doctrine service from the container.
    2. Yes, if you've got a different view which you would like to render, what you've done is perfectly acceptable.

    As an alternative though, you can simply extend the FOSUserBundle with your own, as it appears that you may have already done, by specifying the FOSUserBundle as the parent to your bundle, e.g.

    class MyUserBundle extends Bundle
    {
        public function getParent()
        {
            return 'FOSUserBundle';
        }
    }
    

    Doing this should allow you to simply override the resources by placing your custom views like register.html.twig (if you're using twig) in the same location as those found in the source bundle. This way you can keep the behavior without having to modify controllers and, at the same time, customize your views.

    Of course, if you need additional business logic, or need to do anything custom, you will probably have to override the controller any way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程