FosUserBundle与FosRestBundle集成

jin*_*ini 5 php symfony fosuserbundle

有没有人有一个例子或任何想法如何与FOSUserBundle一起实现FOSRestBundle.我有一个已经用Symfony 2和FOSUserBundle开发的Web应用程序,但我想为api层添加FOSRestBundle.我希望能够传递一个用户名和密码,并从FOSUserBundle接收某种类型的令牌,该令牌代表登录用户,然后我可以在其他api调用之间来回传递.有谁知道这样做的好方法?

Bor*_*éry 3

FOSUserBundle本质上应该是“restful”,这意味着它可以遵循 REST 建议。

然而,它并不是设计用于本地工作FOSRestBundle,最简单的方法是覆盖 Bundle 中的 UsersController 并调整您的操作。

例如,要允许RESTFul注册,您可以编写以下操作:

    public function postUsersAction()
    {
        $form = $this->container->get('fos_user.registration.form');
        $formHandler = $this->container->get('fos_user.registration.form.handler');
        $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

        $process = $formHandler->process($confirmationEnabled);

        if ($process) {
            $user = $form->getData();
            $authUser = false;

            if ($confirmationEnabled) {
            } else {
                $authUser = true;
            }

            $response = new Response();

            if ($authUser) {
                /* @todo Implement authentication */
                //$this->authenticateUser($user, $response);
            }

            $response->setStatusCode(Codes::HTTP_CREATED);
            $response->headers->set(
                'Location',
                $this->generateUrl(
                    'api_users_get_user',
                    array('user' => $user->getId()),
                    true
                )
            );

            return $response;
        }

        return RestView::create($form, Codes::HTTP_BAD_REQUEST);
    }
Run Code Online (Sandbox Code Playgroud)