How to specify default null parameter in xml routing file

This code produces the correct $idtype variable null:

/**
 * Show user
 *
 * @Route("/show/{id}", name="acme_user_show", defaults={"id"=null}, requirements={"id"="\d+"})
 */
public function showUserAction($id = null)
{
    var_dump($id);
}

, while the following code gives a variable of $idtype string:string(4) "null"

/**
 * Show user
 *
 */
public function showUserAction($id = null)
{
    var_dump($id);
}

routing.xml

<route id="acme_user_show" pattern="/show/{id}">
    <default key="_controller">AcmeUserBundle:User:show</default>
    <default key="id">null</default>
    <requirement key="id">\d+</requirement>
</route>

I would suggest that 2 would give similar results, is that normal? How to give default null value in xml?


  • I visit a path /showUserto check if the value of the variable $idis null.
  • I also tried <default key="id" />instead of <default key="id">null</default>=> without success
+5
source share
2 answers

To clarify Patt's answer, this has been fixed, the correct way to indicate this is:

 <default key="threadId" xsi:nil="true" />

Thus, the full route specification in your case will look like this:

<route id="acme_user_show" pattern="/show/{id}">
    <default key="_controller">AcmeUserBundle:User:show</default>
    <default key="id" xsi:nil="true" />
    <requirement key="id">\d+</requirement>
</route>
+6

xml (). . .

@Aitboudad: xml- , xml: xsi: nil = "true".

+2

All Articles