Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

optional parameters in routes defined through annotations

Tags:

symfony

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

like image 473
room13 Avatar asked Aug 08 '11 14:08

room13


People also ask

How do I make params optional in react router?

To add in an optional path parameter in React Router, you just need to add a ? to the end of the parameter to signify that it is optional. And then in your component you could access the GET parameters using the location.search prop that is passed in by React Router.

Is a optional parameter in URL?

Parameters provide a way of passing arbitrary data to a page via the URL. Optional parameters allow URLs to matched to routes even if no parameter value is passed. Things can get a bit complicated if you want to permit multiple optional parameters.

What is Symfony annotation?

As you might know, Symfony2 uses annotations for Doctrine mapping information and validation configuration. Of course, it is entirely optional, and the same can be done with XML, YAML, or even PHP. But using annotations is convenient and allows you to define everything in the same file.

How does Symfony routing work?

Symfony provides a Routing component which allows us, for a HTTP request/URL, to execute a specific function (also known as "Controller"). Note: Controllers must be a callable, for example: an anonymous function: $controller = function (Request $request) { return new Response() }; .


1 Answers

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.

like image 89
Tjorriemorrie Avatar answered Oct 10 '22 13:10

Tjorriemorrie