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

1 Answer 1

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.

2
  • 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, 2011 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, 2011 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.