Skip to content

Commit 7bfdc25

Browse files
committed
Generate non absolute URL's for existing routes
1 parent 1815ca0 commit 7bfdc25

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/LocalizedUrlGenerator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ protected function generateFromUrl($locale = null, $parameters = null, $absolute
134134
$urlParts['path'] = $this->localizeUrlPath($currentPath, $locale);
135135
}
136136

137-
return $this->unparseUrl($urlParts);
137+
return $this->unparseUrl($urlParts, $absolute);
138138
}
139139

140140
/**
@@ -170,12 +170,15 @@ protected function localizeUrlPath($path, $requestedLocale)
170170
* Create a string from parsed URL parts.
171171
*
172172
* @param array $parts
173+
* @param bool $absolute
173174
*
174175
* @return string
175176
*/
176-
protected function unparseUrl(array $parts)
177+
protected function unparseUrl(array $parts, $absolute = true)
177178
{
178-
return $parts['scheme'] . '://' . $parts['host'] . ($parts['port'] ?? '') . ($parts['path'] ?? '');
179+
$host = $absolute ? $parts['scheme'] . '://' . $parts['host'] . ($parts['port'] ?? '') : '';
180+
181+
return $host . ($parts['path'] ?? '');
179182
}
180183

181184
/**

tests/Unit/Macros/LocalizedUrlMacroTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,32 @@ public function it_returns_a_localized_url_for_a_non_localized_fallback_route_wh
524524
], $response->original);
525525
}
526526

527+
/** @test */
528+
public function it_generates_non_absolute_urls_for_existing_routes()
529+
{
530+
$this->setSupportedLocales(['en', 'nl']);
531+
$this->setUseLocaleMiddleware(true);
532+
$this->setAppLocale('en');
533+
534+
Route::localized(function () {
535+
Route::get('route', function () {
536+
return [
537+
'current' => Route::localizedUrl(null, [], false),
538+
'en' => Route::localizedUrl('en', [], false),
539+
'nl' => Route::localizedUrl('nl', [], false),
540+
];
541+
});
542+
});
543+
544+
$response = $this->call('GET', '/nl/route');
545+
$response->assertOk();
546+
$this->assertEquals([
547+
'current' => '/nl/route',
548+
'en' => '/en/route',
549+
'nl' => '/nl/route',
550+
], $response->original);
551+
}
552+
527553
/**
528554
* Set a custom view path so Laravel will find our custom 440 error view.
529555
*

0 commit comments

Comments
 (0)