29

is there a more elegant way to define optional parameters in annotated routes then to define 2 annotations?

Here's how I did it:

/**
*
* @Route("/view/{lang}/{file}", name="legacy_translation_view_file")
* @Route("/view/{lang}", name="legacy_translation_view")
* @Template()
*/
public function viewAction($lang,$file=null)
{
   ...
}

i've seen that the annotation class has a field named "defaults" but am not quiet sure about the syntax

thx

room13
  • 1,922
  • 1
  • 15
  • 28

1 Answers1

49

Symfony has a page on @Route:

E.g maybe you can try.

/**
 * @Route("/{id}/{lang}/{file}", requirements={"id" = "\d+"}, defaults={"file" = null})
 */
public function showAction($id, $lang, $file)
{
}

If null doesn't work try an empty string.

Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131
  • as allways it's only a matter of knowing where to find the information. i have never before looked into the bundle reference. thx – room13 Aug 18 '11 at 11:52
  • 6
    I was going to ask the same thing.. but if you have multiple null or empty string defaults it creates paths like /1/en///1234/23 and Symfony complains about no matching route. – Hades Nov 13 '11 at 23:50