Skip to content

Commit 7122e2f

Browse files
committed
Return the correct URL when not passing a locale to Route::localizedUrl()
1 parent 83a1e62 commit 7122e2f

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/LocalizedUrlGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,16 @@ protected function routeExists()
105105
*/
106106
protected function generateFromUrl($locale = null, $parameters = null, $absolute = true)
107107
{
108-
$locale = $locale ?: App::getLocale();
109108
$currentUrl = Request::fullUrl();
109+
110+
// If its a default 404 (no route exists)
111+
// and there is no specific locale requested
112+
// return the current URL.
113+
if ( ! $this->routeExists() && $locale === null) {
114+
return $currentUrl;
115+
}
116+
117+
$locale = $locale ?: App::getLocale();
110118
$urlParts = parse_url($currentUrl);
111119
$domains = $this->getCustomDomains();
112120

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{ App::getLocale() }}
1+
{{ $response ?? App::getLocale() }}

tests/Unit/Macros/LocalizedUrlMacroTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,23 @@ public function the_macro_does_not_blow_up_on_a_default_404_error()
298298
$response->assertResponseHasNoView();
299299
}
300300

301+
/** @test */
302+
public function a_404_receives_the_correct_localized_url_from_a_view_composer()
303+
{
304+
$this->setSupportedLocales(['en', 'nl']);
305+
$this->setAppLocale('en');
306+
$this->setCustomErrorViewPath();
307+
308+
View::composer('*', function ($view) {
309+
$view->with('response', Route::localizedUrl());
310+
});
311+
312+
$response = $this->get('/nl/route/does/not/exist');
313+
$response->assertNotFound();
314+
$response->assertResponseHasNoView();
315+
$this->assertEquals(url('/nl/route/does/not/exist'), trim($response->original));
316+
}
317+
301318
/** @test */
302319
public function a_404_is_not_localized_when_triggered_by_a_non_existing_route()
303320
{

0 commit comments

Comments
 (0)