Reputation: 172
i created a route with optional parameter in controller like this:
/**
* 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)
but if i try to create a link with this named route, the optional parameter is ommitted:
{{ path('league', {'assoc': association.short, 'league': league.id, 'game': g.id}) }}
resulting link is
/association/BVNR/7
What am i missing?
Upvotes: 3
Views: 13042
Reputation: 13891
In the following definitions,
* @Route("/association/{assoc}/{league}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null})
* @Route("/association/{assoc}/{league}/{game}")
two routes are related to your action, the first one (named "league"
which doesn't have any default parameter and a second unnamed one (as you didn't add name attribute) which also doesn't have any default parameter.
How to fix ...
name
to your second route and call it as it contains "game"
parameter."game"
parameter to your second route (As it the only one to have a game
parameter."How to improve ..."
part of my answer).Try this ...
* @Route("/association/{assoc}/{league}/{game}", name="league_game", requirements={"league" = "\d+"}, defaults={"game" = null})
While you should call "league_game"
instead of "league"
,
{{ path('league_game', {'assoc': association.short, 'league': league.id, 'game': g.id}) }}
How to improve ...
Make sure you really need to define two routes, because I would suggest keeping only one route.
As there's a default value for "game"
in the following definition,
@Route("/association/{assoc}/{league}/{game}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null}
It then covers both versions, with and without "game"
.
Upvotes: 4