2016-05-20 8 views
1

私はFOSRestBundleでSymfony2を使用しています。注釈を使用せずに@QueryParamアノテーションと@RequestParamアノテーションの機能を持つことは可能ですか?Symfony - 注釈のないFOSRestBundleとbody_listenerのフィルタリクエスト?

json api(format)をビルドしようとしています。そのため、include、page、filter、fields、sortなどのクエリパラメータを許可します。

  1. これを検出するためにformat_listenerを使用するのはjsonです。
  2. カスタムbody_listener jsonハンドラを使用して、リクエストを処理して、thisに似ているようにします。
  3. アクション・ファンクション内の問合せ/リクエスト・パラメータをコントローラで検証し、例外コントローラが無効な場合は、例外コントローラによって処理されるように例外をスローします。 (body_listenerは、コントローラの要求からデータを簡単に抽出するためのヘルパーとして機能しますが、コントローラはそのデータの処理を決定します)。

カスタムbody_listenerを作成します。私はカスタムデコーダやノーマライザを作る必要があるかどうか、どのような例がないのでそのクラスはどのように見えるかわからない。

コントローラがどのように見えるかのラフコード:あなたはのparamフェッで動的にパラメータを定義することができます

<?php 
namespace CoreBundle\Controller; 

use FOS\RestBundle\Controller\FOSRestController; 
use FOS\RestBundle\View\View; 
use FOS\RestBundle\Context\Context; 
use Psr\Http\Message\ServerRequestInterface; 

class SiteController extends FOSRestController 
{ 
    public function getAction($id, ServerRequestInterface $request) 
    { 
     try { 
      // Validate $request. This is where the query/request 
      // param annotation functionality would be replaced. 
     } catch (Exception $e) { 
      throw new InvalidRequestException($e); 
     } 

     $siteService = $this->get('app.site_service'); 
     $site = $siteService->getSite($id); 

     $context = new Context(); 

     $context->setVersion($request->getVersion()); 
     // Ex: /sites/63?fields[sites]=name,address&fields[company]=foo,bar 
     if ($request->hasIncludeFields()) { 
      $context->addAttribute('include_fields', $request->getIncludeFields()); // Or however to do this 
     } 

     $view = new View($site, 200); 

     $view->setContext($context); 

     return $view; 
    } 
} 

答えて

1

に。それはdocumentationに記載されています。注釈付き

:たとえば

注釈なし

<?php 

namespace ContentBundle\Controller\API; 

use FOS\RestBundle\Controller\FOSRestController; 
use FOS\RestBundle\Controller\Annotations\QueryParam; 
use FOS\RestBundle\Request\ParamFetcher; 

class PostController extends FOSRestController 
{ 
    /** 
    * @QueryParam(name="sort", requirements="(asc|desc)", allowBlank=false, default="asc", description="Sort direction") 
    */ 
    public function getPostsAction(ParamFetcher $paramFetcher) 
    { 
     $sort = $paramFetcher->get('sort'); 
    } 
} 

<?php 

namespace ContentBundle\Controller\API; 

use FOS\RestBundle\Controller\FOSRestController; 
use FOS\RestBundle\Controller\Annotations\QueryParam; 
use FOS\RestBundle\Request\ParamFetcher; 

class PostController extends FOSRestController 
{ 
    public function getPostsAction(ParamFetcher $paramFetcher) 
    { 
     $sort = new QueryParam(); 
     $sort->name = 'sort'; 
     $sort->requirements = '(asc|desc)'; 
     $sort->allowBlank = false; 
     $sort->default = 'asc'; 
     $sort->description = 'Sort direction'; 

     $paramFetcher->addParam($sort); 

     $param = $paramFetcher->get('sort'); 
     // 
    } 
} 
+0

それはコントローラがParamFetcherとRequestオブジェクトを取得することは可能ですか? – GreeKatrina

+1

@GreeKatrinaはい、通常のsymfonyコントローラと同じです –

関連する問題