3

次のように、コントローラーでオプションのパラメーターを使用してルートを作成しました。

/**
 * League action
 *
 * @Route("/association/{assoc}/{league}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null})
 * @Route("/association/{assoc}/{league}/{game}")
 * @Template()
 *
 * @param $assoc
 * @param $league
 * @param $game
 * @return array
 */
 public function leagueAction($assoc, $league, $game)

ただし、この名前付きルートでリンクを作成しようとすると、オプションのパラメーターが省略されます。

{{ path('league', {'assoc': association.short, 'league': league.id, 'game': g.id}) }}

結果のリンクは

/協会/BVNR/7

私は何が欠けていますか?

4

1 に答える 1

4

以下の定義では、

* @Route("/association/{assoc}/{league}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null})
* @Route("/association/{assoc}/{league}/{game}")

2 つのルートがアクションに関連しています。最初のルート ("league"デフォルト パラメータを持たない名前付きルート) と、デフォルト パラメータを持たない名前のないルート (名前属性を追加しなかったため) です。

直し方 ...

  • name2 番目のルートに a を追加し、"game"パラメータが含まれているので呼び出します。
  • パラメータのデフォルト値を"game"2 番目のルートに移動します (パラメータを持つ唯一のルートgameです。
  • (実際には 2 つのルートを定義する必要はありません"How to improve ..."。私の回答の一部を見てください)。

これを試して ...

 * @Route("/association/{assoc}/{league}/{game}", name="league_game", requirements={"league" = "\d+"}, defaults={"game" = null})

"league_game"の代わりに呼び出す必要がありますが"league"

{{ path('league_game', {'assoc': association.short, 'league': league.id, 'game': g.id}) }}

改善方法 ...

2 つのルートを定義する必要があることを確認してください。1 つのルートのみを保持することをお勧めします。

"game"以下の定義にはのデフォルト値があるので、

@Route("/association/{assoc}/{league}/{game}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null}

次に、 がある場合とない場合の両方のバージョンについて説明します"game"

于 2013-09-09T08:22:24.247 に答える