Skip to content

Commit 0a1b6fc

Browse files
committed
Ignore query string when passed as parameters to route()
1 parent f5071d5 commit 0a1b6fc

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/LocalizedUrlGenerator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ public function generateFromRequest($locale = null, $parameters = null, $absolut
6363
}
6464

6565
if ($url = $this->generateFromNamedRoute($locale, $parameters, $absolute)) {
66-
return empty($query) && $keepQuery ? $url . $urlBuilder->getQueryString() : $url;
66+
$url = empty($query) ? $url . $urlBuilder->getQueryString() : $url;
67+
$startQueryString = strpos($url, '?');
68+
69+
return ($keepQuery === false && $startQueryString !== false)
70+
? substr($url, 0, $startQueryString)
71+
: $url;
6772
}
6873

6974
$urlBuilder->setPath($this->replaceParameters($this->route->uri(), $slugs));

tests/Unit/Macros/LocalizedUrlMacroTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,32 @@ public function it_returns_a_url_with_query_string_for_existing_localized_named_
820820
], $response->original);
821821
}
822822

823+
/** @test */
824+
public function it_returns_a_url_without_query_string_for_existing_localized_named_routes()
825+
{
826+
$this->withoutExceptionHandling();
827+
$this->setSupportedLocales(['en', 'nl']);
828+
$this->setAppLocale('en');
829+
830+
Route::localized(function () {
831+
Route::get('route', function () {
832+
return [
833+
'current' => Route::localizedUrl(null, null, true, $keepQuery = false),
834+
'en' => Route::localizedUrl('en', null, true, $keepQuery = false),
835+
'nl' => Route::localizedUrl('nl', null, true, $keepQuery = false),
836+
];
837+
})->name('route');
838+
});
839+
840+
$response = $this->call('GET', '/nl/route?another=one&param=value');
841+
$response->assertOk();
842+
$this->assertEquals([
843+
'current' => url('/nl/route'),
844+
'en' => url('/en/route'),
845+
'nl' => url('/nl/route'),
846+
], $response->original);
847+
}
848+
823849
/** @test */
824850
public function it_accepts_query_string_parameters_using_named_routes()
825851
{
@@ -843,6 +869,29 @@ public function it_accepts_query_string_parameters_using_named_routes()
843869
], $response->original);
844870
}
845871

872+
/** @test */
873+
public function it_ignores_query_string_parameters_using_named_routes()
874+
{
875+
$this->withoutExceptionHandling();
876+
$this->setSupportedLocales(['en', 'nl']);
877+
878+
Route::get('route/{slug}/{optional?}', function () {
879+
return [
880+
'current' => Route::localizedUrl(null, ['another-slug', 'optional-slug', 'new' => 'value'], true, $keepQuery = false),
881+
'en' => Route::localizedUrl('en', ['another-slug', 'optional-slug', 'new' => 'value'], true, $keepQuery = false),
882+
'nl' => Route::localizedUrl('nl', ['another-slug', 'optional-slug', 'new' => 'value'], true, $keepQuery = false),
883+
];
884+
})->name('route');
885+
886+
$response = $this->call('GET', '/route/some-slug?param=value');
887+
$response->assertOk();
888+
$this->assertEquals([
889+
'current' => url('/route/another-slug/optional-slug'),
890+
'en' => url('/route/another-slug/optional-slug'),
891+
'nl' => url('/route/another-slug/optional-slug'),
892+
], $response->original);
893+
}
894+
846895
/** @test */
847896
public function it_accepts_query_string_parameters_using_unnamed_routes()
848897
{
@@ -866,6 +915,29 @@ public function it_accepts_query_string_parameters_using_unnamed_routes()
866915
], $response->original);
867916
}
868917

918+
/** @test */
919+
public function it_ignores_query_string_parameters_using_unnamed_routes()
920+
{
921+
$this->withoutExceptionHandling();
922+
$this->setSupportedLocales(['en', 'nl']);
923+
924+
Route::get('route/{slug}/{optional?}', function () {
925+
return [
926+
'current' => Route::localizedUrl(null, ['another-slug', 'optional-slug', 'new' => 'value'], true, $keepQuery = false),
927+
'en' => Route::localizedUrl('en', ['another-slug', 'optional-slug', 'new' => 'value'], true, $keepQuery = false),
928+
'nl' => Route::localizedUrl('nl', ['another-slug', 'optional-slug', 'new' => 'value'], true, $keepQuery = false),
929+
];
930+
});
931+
932+
$response = $this->call('GET', '/route/some-slug?param=value');
933+
$response->assertOk();
934+
$this->assertEquals([
935+
'current' => url('/route/another-slug/optional-slug'),
936+
'en' => url('/route/another-slug/optional-slug'),
937+
'nl' => url('/route/another-slug/optional-slug'),
938+
], $response->original);
939+
}
940+
869941
/** @test */
870942
public function it_allows_optional_parameters_with_named_routes()
871943
{

0 commit comments

Comments
 (0)