dtxpz8785 2013-07-10 07:32
浏览 23
已采纳

访问服务覆盖FOS用户捆绑控制器

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

  • 写回答

1条回答 默认 最新

  • dpcj32769 2013-07-10 07:38
    关注

    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错