Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give a null default parameter in an xml routing file

Tags:

symfony

This code dumps the correct $id variable of type null:

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

whereas the following code gives an $id variable of type 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 assume the 2 to give similar results, is this normal? How would one give a default null value in xml?


  • I visit the /showUser path to test if $id variable is null.
  • I also tried <default key="id" /> instead of <default key="id">null</default> => no success
like image 299
Mick Avatar asked Mar 19 '13 06:03

Mick


2 Answers

Just to clarify Patt's answer, this was fixed, the correct way to specify it is like this:

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

So full route specification in your case would be 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>
like image 113
Marius Balčytis Avatar answered Oct 24 '22 18:10

Marius Balčytis


This feature is not implemented in xml (yet). See this bug report.

@Aitboudad: The routing xml loader does not support null value, the correct way to represent null xml elements is xsi:nil="true".

like image 29
Mick Avatar answered Oct 24 '22 18:10

Mick