简体   繁体   中英

Access to service overriding FOS User Bundle Controller

I'm new in Symfony2, I'm trying to override a controller using a service inside

This is the register controller

class RegistrationController extends BaseController
{
  public function registerAction(Request $request)
  {
    /** @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);

    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, new UserEvent($user, $request));

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

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

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

            $userManager->updateUser($user);

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

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

            //create UserInfo
            $doctrine = $this->container->get('doctrine');

            $userInfo = new UserInformation();
            $userInfo->setUser($user);

            //save the userInfo
            $em = $doctrine->getManager();
            $em->persist($userInfo);
            $em->flush();

            //add user first login
            $loginManager = $this->get('user_login_manager');
            $loginManager->saveUser($request, $user);

            return $response;
        }
    }

    return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
            'form' => $form->createView(),
    ));
  }
}

near the end I'm using

$loginManager = $this->get('user_login_manager');
$loginManager->saveUser($request, $user);

But I can't use get because this is not extending Controller. So I don't know how to access to my service in this controller

Thanks

$this->get('some_service') is only a helper shortcut defined in the symfony base controller. Look at you code above and see how all the services are called:

$loginManager = $this->container->get('user_login_manager');

Btw. if you are using the latest version of FOSUserBundle ( dev-master ), then the new event system might fit better than overriding the controller. REGISTER_COMPLETED may fit for you use case. If you take a look in the controller code above, you can see, when the event is dispatched. You should fairly use events than controller overriding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM