Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

routing parameter is once equal to undefined and nex to "undefined" #214

Closed
NadjibBELLOUNDJA opened this issue Oct 24, 2015 · 2 comments
Closed

Comments

@NadjibBELLOUNDJA
Copy link

Hi,

I have a route in my symfony project that I wrote below:

at_platform_update_towns_list:
    path: /updatetownslist/{regionRealName}/{index}
    defaults: {_controller: ATPlatformBundle:CreateSession:updateTownsList, regionRealName: null , index: 1}

And in my javascript, I have got this function:

function getTownsList(regionName)
{
    var updateTownsListRoute = Routing.generate('at_platform_update_towns_list', {'regionRealName': undefined, 'index': 1});
    console.log(updateTownsListRoute);

    var updateTownsListRoute = Routing.generate('at_platform_update_towns_list', {'regionRealName': undefined, 'index': 2});
    console.log(updateTownsListRoute);
}

And this is what my console prints:


/app_dev.php/platform/updatetownslist
default-functions.js:223 /app_dev.php/platform/updatetownslist/undefined/2

I have two questions/issues about theses facts.

  1. How come in the first console.log, the undefined parameter has no value and the second has literally the "undefined" string value? I would like my second generated rule to be the same as the first one, how can I do it?

  2. Why is the index parameter ignored in the routing generation when the first parameter is undefined? Can someone help me to add the second routing paramater even if the first is undefined?

Thanks.

@stof
Copy link
Member

stof commented Oct 25, 2015

The index parameter is not ignored in the routing when the first parameter is undefined. It is removed from the generated URL when it is equal to its default value, which is the behavior of the Symfony router too (which is why the JS does it).
The URL generated with index = 2 cannot be the same than the first one, because you would get index = 1 on the PHP side when matching the URL.

And regarding the first parameter, you see undefined in the second case, because this is what casting undefined to string produces in JS. In the first case, you don't have it, because it seems like the JS code considers that undefined is equal to the null default value, and so skips the optional parameter (because all optional parameters after it are skipped).

@NadjibBELLOUNDJA
Copy link
Author

Ok, it is clearer now.

Thanks @stof

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants