2012-05-15 6 views
5

FOSRserBundleと一緒にFOSRestBundleをどのように実装するのかの例がありますか? Symfony 2とFOSUserBundleで既に開発されたWebアプリケーションがありますが、私はAPIレイヤーにFOSRestBundleを追加したいと思います。私はそれにユーザー名とパスワードを渡し、ログインしたユーザーを表すFOSUserBundleからいくつかのタイプのトークンを受け取って、他のapi呼び出しの間でやり取りできるようにしたいと考えています。誰もがこれを行う良い方法を知っていますか?FosRserBundleとFosRestBundleの統合

+1

を、いくつかのセキュリティ上の問題がありました。詳細については、Symfony Blogをご覧ください:http://symfony.com/blog/security-release-fosuserbundle – F481

答えて

3

FOSUserBundleは、ネイティブに「安らか」である必要があります。つまり、RESTの推奨に従うことを意味します。

FOSRestBundleでネイティブに動作するようには設計されていませんが、これを行う最も簡単な方法は、バンドルのUsersControllerをオーバーライドしてアクションを適用することです。例えば

、RESTfulな登録を可能にするために、次のアクション書くことがあります。最新バージョンにFOSUserBundleを更新するために、心に留めておくちなみに

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); 
    }