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