Skip to content

Commit fac6e4a

Browse files
committed
Handle duplicate route and query string parameter keys
1 parent 8bdb931 commit fac6e4a

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/LocalizedUrlGenerator.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,9 @@ public function generateFromRequest(string $locale = null, $parameters = null, b
8383
$this->determineQueryStringParameters($requestQueryString, $queryStringParameters, $keepQuery)
8484
);
8585

86-
// Merge the route parameters with the query string parameters, if any.
87-
$namedRouteParameters = array_merge($routeParameters, $urlBuilder->getQuery());
88-
8986
// Generate the URL using the route's name, if possible.
90-
if ($url = $this->generateNamedRouteURL($locale, $namedRouteParameters, $absolute)) {
91-
return $url;
87+
if ($url = $this->generateNamedRouteURL($locale, $routeParameters, $absolute)) {
88+
return $urlBuilder->getQueryString() ? $url . '?' . $urlBuilder->getQueryString() : $url;
9289
}
9390

9491
// If a named route could not be resolved, replace the parameter

tests/Feature/Macros/Route/LocalizedUrlMacroTest.php

+68
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,74 @@ public function it_ignores_query_string_parameters_using_unnamed_routes()
987987
], $response->original);
988988
}
989989

990+
/** @test */
991+
public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_unnamed_routes()
992+
{
993+
$this->withoutExceptionHandling();
994+
$this->setSupportedLocales(['en', 'nl']);
995+
996+
$model = (new ModelOneWithRouteBinding([
997+
'slug' => [
998+
'en' => 'en-slug',
999+
'nl' => 'nl-slug',
1000+
],
1001+
]))->setKeyName('slug');
1002+
1003+
App::instance(ModelOneWithRouteBinding::class, $model);
1004+
1005+
Route::localized(function () use ($model) {
1006+
Route::get('route/{slug}', function (ModelOneWithRouteBinding $slug) {
1007+
return [
1008+
'current' => Route::localizedUrl(),
1009+
'en' => Route::localizedUrl('en'),
1010+
'nl' => Route::localizedUrl('nl'),
1011+
];
1012+
})->middleware(['web']);
1013+
});
1014+
1015+
$response = $this->call('GET', '/en/route/en-slug?slug=duplicate');
1016+
$response->assertOk();
1017+
$this->assertEquals([
1018+
'current' => URL::to('/en/route/en-slug?slug=duplicate'),
1019+
'en' => URL::to('/en/route/en-slug?slug=duplicate'),
1020+
'nl' => URL::to('/nl/route/nl-slug?slug=duplicate'),
1021+
], $response->original);
1022+
}
1023+
1024+
/** @test */
1025+
public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_named_routes()
1026+
{
1027+
$this->withoutExceptionHandling();
1028+
$this->setSupportedLocales(['en', 'nl']);
1029+
1030+
$model = (new ModelOneWithRouteBinding([
1031+
'slug' => [
1032+
'en' => 'en-slug',
1033+
'nl' => 'nl-slug',
1034+
],
1035+
]))->setKeyName('slug');
1036+
1037+
App::instance(ModelOneWithRouteBinding::class, $model);
1038+
1039+
Route::localized(function () use ($model) {
1040+
Route::get('route/{slug}', function (ModelOneWithRouteBinding $slug) {
1041+
return [
1042+
'current' => Route::localizedUrl(),
1043+
'en' => Route::localizedUrl('en'),
1044+
'nl' => Route::localizedUrl('nl'),
1045+
];
1046+
})->middleware(['web'])->name('test');
1047+
});
1048+
1049+
$response = $this->call('GET', '/en/route/en-slug?slug=duplicate');
1050+
$response->assertOk();
1051+
$this->assertEquals([
1052+
'current' => URL::to('/en/route/en-slug?slug=duplicate'),
1053+
'en' => URL::to('/en/route/en-slug?slug=duplicate'),
1054+
'nl' => URL::to('/nl/route/nl-slug?slug=duplicate'),
1055+
], $response->original);
1056+
}
1057+
9901058
/** @test */
9911059
public function it_allows_optional_parameters_with_named_routes()
9921060
{

0 commit comments

Comments
 (0)