1
私はFOSRestBundleでSymfony2を使用しています。注釈を使用せずに@QueryParamアノテーションと@RequestParamアノテーションの機能を持つことは可能ですか?Symfony - 注釈のないFOSRestBundleとbody_listenerのフィルタリクエスト?
json api(format)をビルドしようとしています。そのため、include、page、filter、fields、sortなどのクエリパラメータを許可します。
- これを検出するためにformat_listenerを使用するのはjsonです。
- カスタムbody_listener jsonハンドラを使用して、リクエストを処理して、thisに似ているようにします。
- アクション・ファンクション内の問合せ/リクエスト・パラメータをコントローラで検証し、例外コントローラが無効な場合は、例外コントローラによって処理されるように例外をスローします。 (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;
}
}
それはコントローラがParamFetcherとRequestオブジェクトを取得することは可能ですか? – GreeKatrina
@GreeKatrinaはい、通常のsymfonyコントローラと同じです –