2016-06-13 3 views
-1

私はGET要求を処理するコントローラを持っています。私は、例えば、GETリクエストのための要件のパラメータを設定する必要があります。: "http://localhost/site/main?id=10&sort=ascsymfonyのコントローラでget要求パラメータの要件を設定します。

私のコントローラクラスIはそれを行うことができますどのように

class IndexController extends Controller { 
` /** 
    * @Route 
    * (
    *  "/site/main", 
    *  name="main" 
    *) 
    * 
    * @Method("GET") 
    */ 
    public function mainAction(Request $request) 
    { 
     return new Response('', 200); 
    } 
} 

UPDは: "\ dの+"、 ソート: "\ W +" 等 同じことをsymfonyはPOSTリクエストを行うことができますように私は IDのようなURLパラメータのための要件を設定する必要があります。

+0

このページをお読みになりましたか? http://symfony.com/doc/current/book/routing.html – BentCoder

+2

ルーティングのためのドキュメント、具体的には要件についてはこちらをご覧ください:http://symfony.com/doc/current/book/routing.html#adding -requirements –

+0

@EmanuelOster私の場合の解決策は見つかりませんでした。そうした場合は、解決策を提示してください。 – eatmypants

答えて

0

@Methodは、あなたがPOSTでこのルートを使用しようとする場合は、あなたはあなたがこのような「@Route」の注釈で要件を指定することができます404

+0

は質問を更新します。私はあなたが私を誤解したと思う。 – eatmypants

1

ありますhttp://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-method

を必要とするものである。

class IndexController extends Controller { 

` /** 
    * @Route 
    * (
    *  "/site/main", 
    *  name="main", 
    *  requirements={ 
    *  "id": "\d+", 
    *  "sort": "\w+" 
    * }) 
    *) 
    * 
    * @Method("GET") 
    */ 
    public function mainAction(Request $request) 
    { 
     return new Response('', 200); 
    } 
} 
+0

試しましたか?私のために働かなかった。 – eatmypants

0

あなたの質問をよく理解できませんでした。 しかし、もしあなたが必要とするのは、GETメソッドのパラメータのためのフィルタ機構を、すでに経路要求を使ってURLで利用できるように設定することであるならば、私はRouteコンポーネント内でこれにツールを使う準備ができていないと思います。@Yoshi

私はこのような仕事を自分でやっていました。それがあなたにも役立つことを願っています

public function indexAction(Request $request) 
{ 
    // Parameter names used in the current request 
    $current_request_params=array_keys($request->query->all()); 

    // $ALLOWED_INDEX_PARAMS should be declared as Class static property array and hold names of the query parameters you want to allow for this method/action 
    $unallowed_request_params=array_diff($current_request_params,PersonController::$ALLOWED_INDEX_PARAMS); 

    if (!empty($unallowed_request_params)) 
    { 
     $result=array("error"=>sprintf("Unknown parameters: %s. PLease check the API documentation for more details.",implode($unallowed_request_params,", "))); 

     $jsonRsp=$this->get("serializer")->serialize($result,"json"); 

     return new Response($jsonRsp,Response::HTTP_BAD_REQUEST,array("Content-Type"=>"application/json")); 
    } 
    // We are sure all parameters are correct, process the query job .. 
} 
関連する問題