Question

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) ?

Was it helpful?

Solution

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.

OTHER TIPS

To answer your first question - you have $this->container inside the controller, so, you could access Doctrine using $doctrine = $this->container->get('doctrine');

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top