diff --git a/src/LocalizedUrlGenerator.php b/src/LocalizedUrlGenerator.php index 4282bc2..d8c8099 100644 --- a/src/LocalizedUrlGenerator.php +++ b/src/LocalizedUrlGenerator.php @@ -83,12 +83,9 @@ public function generateFromRequest(string $locale = null, $parameters = null, b $this->determineQueryStringParameters($requestQueryString, $queryStringParameters, $keepQuery) ); - // Merge the route parameters with the query string parameters, if any. - $namedRouteParameters = array_merge($routeParameters, $urlBuilder->getQuery()); - // Generate the URL using the route's name, if possible. - if ($url = $this->generateNamedRouteURL($locale, $namedRouteParameters, $absolute)) { - return $url; + if ($url = $this->generateNamedRouteURL($locale, $routeParameters, $absolute)) { + return $urlBuilder->getQueryString() ? $url . '?' . $urlBuilder->getQueryString() : $url; } // If a named route could not be resolved, replace the parameter diff --git a/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php b/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php index 4230317..9e3f2ce 100644 --- a/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php +++ b/tests/Feature/Macros/Route/LocalizedUrlMacroTest.php @@ -987,6 +987,74 @@ public function it_ignores_query_string_parameters_using_unnamed_routes() ], $response->original); } + /** @test */ + public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_unnamed_routes() + { + $this->withoutExceptionHandling(); + $this->setSupportedLocales(['en', 'nl']); + + $model = (new ModelOneWithRouteBinding([ + 'slug' => [ + 'en' => 'en-slug', + 'nl' => 'nl-slug', + ], + ]))->setKeyName('slug'); + + App::instance(ModelOneWithRouteBinding::class, $model); + + Route::localized(function () use ($model) { + Route::get('route/{slug}', function (ModelOneWithRouteBinding $slug) { + return [ + 'current' => Route::localizedUrl(), + 'en' => Route::localizedUrl('en'), + 'nl' => Route::localizedUrl('nl'), + ]; + })->middleware(['web']); + }); + + $response = $this->call('GET', '/en/route/en-slug?slug=duplicate'); + $response->assertOk(); + $this->assertEquals([ + 'current' => URL::to('/en/route/en-slug?slug=duplicate'), + 'en' => URL::to('/en/route/en-slug?slug=duplicate'), + 'nl' => URL::to('/nl/route/nl-slug?slug=duplicate'), + ], $response->original); + } + + /** @test */ + public function it_prefers_route_parameters_before_query_string_parameters_with_the_same_name_in_named_routes() + { + $this->withoutExceptionHandling(); + $this->setSupportedLocales(['en', 'nl']); + + $model = (new ModelOneWithRouteBinding([ + 'slug' => [ + 'en' => 'en-slug', + 'nl' => 'nl-slug', + ], + ]))->setKeyName('slug'); + + App::instance(ModelOneWithRouteBinding::class, $model); + + Route::localized(function () use ($model) { + Route::get('route/{slug}', function (ModelOneWithRouteBinding $slug) { + return [ + 'current' => Route::localizedUrl(), + 'en' => Route::localizedUrl('en'), + 'nl' => Route::localizedUrl('nl'), + ]; + })->middleware(['web'])->name('test'); + }); + + $response = $this->call('GET', '/en/route/en-slug?slug=duplicate'); + $response->assertOk(); + $this->assertEquals([ + 'current' => URL::to('/en/route/en-slug?slug=duplicate'), + 'en' => URL::to('/en/route/en-slug?slug=duplicate'), + 'nl' => URL::to('/nl/route/nl-slug?slug=duplicate'), + ], $response->original); + } + /** @test */ public function it_allows_optional_parameters_with_named_routes() {