laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.5k stars 11.01k forks source link

Route helper function generates different URL addresses for the same parameters #22510

Closed matoh closed 6 years ago

matoh commented 6 years ago

Description:

When generating URL for a given named route with parameters using route() helper function, it happens that URL could be generated with different structure depending on parameters order.

In the documentation, there is no mention of functionality allowing passing parameters that are not defined as route parameters. So i am not sure if these parameters should be automatically added to the URL as query string parameters or not. In case that this behavior of adding query string parameters is expected then generating of URL should be consistent for the defined route parameters.

Steps To Reproduce:

Create new routes and test following examples.

Enter URL address of the route definition bellow: {baseUrl}/test1/SK-GB

Route::get('/test1/{fromCountryCode}-{toCountryCode}', function() {
    $url = route('countries.test1', [
        'currencyCode' => 'EUR',
        'fromCountryCode' => 'SK',
        'toCountryCode' => null,
    ]);

    dd($url);
})->name('countries.test1');

Outputs:

{baseUrl}/test1/SK-EUR?

Enter URL address of the route definition bellow: {baseUrl}/test2/SK-GB

Route::get('/test2/{fromCountryCode}-{toCountryCode}', function() {
    $url = route('countries.test2', [
        'fromCountryCode' => 'SK',
        'toCountryCode' => null,
        'currencyCode' => 'EUR',
    ]);

    dd($url);
})->name('countries.test2');

Outputs

{baseUrl}/test2/SK-?currencyCode=EUR

When route parameter value toCountryCode is empty string instead of null, both URL addresses will be generated with the same structure.

themsaid commented 6 years ago

The use case isn't supported, a route parameter is a separate segment of the URI /{fromCountryCode}/{toCountryCode}.

Having two named parameters in one segment won't work at the moment.

matoh commented 6 years ago

Ok thank you for your answer but the output is the same when i tried to do the examples mentioned above with 2 segments for country codes - /{fromCountryCode}/{toCountryCode}.

Is it allowed to pass multiple parameters(named parameters for route and parameters that will be converted to query string) to route helper function?

I found examples of doing it in Laracast discussions and i am not sure if it is the right way to go as this functionality of route helper function is not documented.

Thanks