简体   繁体   English

FosUserBundle与FosRestBundle集成

[英]FosUserBundle integration with FosRestBundle

Does anyone have an example or any idea how one would implement the FOSRestBundle together along with the FOSUserBundle. 有没有人有一个例子或任何想法如何与FOSUserBundle一起实现FOSRestBundle。 I have a Web app already developed with Symfony 2 and the FOSUserBundle, but I would like to add the FOSRestBundle for an api layer. 我有一个已经用Symfony 2和FOSUserBundle开发的Web应用程序,但我想为api层添加FOSRestBundle。 I want to be able to pass it a username and password and receive some type of token from the FOSUserBundle that represents the logged in user that I can then pass and forth between other api calls. 我希望能够传递一个用户名和密码,并从FOSUserBundle接收某种类型的令牌,该令牌代表登录用户,然后我可以在其他api调用之间来回传递。 Does anyone know of a good way to do this? 有谁知道这样做的好方法?

FOSUserBundle should be natively "restful" meaning that it may follow the REST recommandations. FOSUserBundle应该是原生的“restful”,这意味着它可以遵循REST建议。

However, it is not designed to work natively with FOSRestBundle , the simplest way to do that is to override the UsersController in your Bundle and adapt your actions. 但是,它不能与FOSRestBundle本地工作,最简单的方法是覆盖Bundle中的UsersController并调整您的操作。

For example, to allow RESTFul registration, you may write the following action: 例如,要允许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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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