3

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?

Steffen Kamper
  • 172
  • 1
  • 2
  • 10

1 Answers1

4

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 ...

  • Add a name to your second route and call it as it contains "game" parameter.
  • Move the default value of "game" parameter to your second route (As it the only one to have a game parameter.
  • (You don't really need to define two routes, take a look at the "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".

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
  • ah - naming second route did the trick. But i have to use defaults also with first route, as symfony claims about required parameter game. So this seems to work: * @Route("/association/{assoc}/{league}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null}) * @Route("/association/{assoc}/{league}/{game}", name="leaguedetails", defaults={"game" = null}) – Steffen Kamper Sep 09 '13 at 08:30
  • ommitting first route doesn't work, i tried all combinations. – Steffen Kamper Sep 09 '13 at 08:41
  • You don't need to define "game" as a default parameter to your first route as it didn't contain any "game" parameter. Then, your first route should be called as follow: {{ path('league', {'assoc': association.short, 'league': league.id}) }} where league.id must fit the "\d+" constraint. – Ahmed Siouani Sep 09 '13 at 08:44
  • Ahmed - if i ommit defaults on first route, i get following error: Controller "...\LigaController::leagueAction()" requires that you provide a value for the "$game" argument (because there is no default value or because there is a non optional argument after this one). though in link generating for first route i don't use parameter game. – Steffen Kamper Sep 09 '13 at 08:58
  • Ah sorry, you're right, I didn't see your action signature. You need to set a value (default or not) for all the parameters used by leagueAction. – Ahmed Siouani Sep 09 '13 at 09:01
  • ah, ok - need to change function parameter to optional, then i can ommit defaults on first route: public function leagueAction($assoc, $league, $game = null) – Steffen Kamper Sep 09 '13 at 09:01
  • Exactly! As it's (well) explained [here](http://symfony.com/doc/current/book/controller.html#route-parameters-controller-arguments). – Ahmed Siouani Sep 09 '13 at 09:05
  • btw, you don't need two route definitions. Because only `game` is optional, one defintion is enough: `@Route("/association/{assoc}/{league}/{game}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null})`. Generate both versions with or without game parameter trough the `league` route. – Emii Khaos Sep 09 '13 at 16:15
  • Yeah, sure. As suggested in the "Also ..." part of my answer. Thanks for making it clearer. – Ahmed Siouani Sep 09 '13 at 16:19
  • Jep, maybe it would be worth an edit, to make this clear. Especially for other users, which are searching on similar issue, that only one route is enough. – Emii Khaos Sep 09 '13 at 16:58