Skip to content

Commit

Permalink
Handle duplicate route and query string parameter keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvermeyen committed May 2, 2023
1 parent 8bdb931 commit fac6e4a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/LocalizedUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions tests/Feature/Macros/Route/LocalizedUrlMacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit fac6e4a

Please sign in to comment.