Skip to content

Commit 6727dc6

Browse files
committed
Fix issue with generating localized URLs using custom domains
1 parent a116e43 commit 6727dc6

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/UrlGenerator.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected function stripLocaleFromRouteName($name)
114114
return $name;
115115
}
116116

117-
$locales = Config::get('localized-routes.supported-locales', []);
117+
$locales = $this->getSupportedLocales();
118118

119119
// If the first part of the route name is a valid
120120
// locale, then remove it from the array.
@@ -127,4 +127,21 @@ protected function stripLocaleFromRouteName($name)
127127

128128
return $name;
129129
}
130+
131+
/**
132+
* Get the supported locales and not the custom domains.
133+
*
134+
* @return array
135+
*/
136+
protected function getSupportedLocales()
137+
{
138+
$locales = Config::get('localized-routes.supported-locales', []);
139+
$keys = array_keys($locales);
140+
141+
if ( ! empty($locales) && is_numeric($keys[0])) {
142+
return $locales;
143+
}
144+
145+
return $keys;
146+
}
130147
}

tests/Unit/UrlGeneratorTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ public function it_gets_the_url_of_a_route_in_the_given_locale()
9494
$this->assertEquals(url('nl/route'), route('nl.route.name', [], true, 'nl'));
9595
}
9696

97+
/** @test */
98+
public function it_gets_the_url_of_a_route_in_the_given_locale_when_using_custom_domains()
99+
{
100+
$this->setSupportedLocales([
101+
'en' => 'en.domain.test',
102+
'nl' => 'nl.domain.test',
103+
]);
104+
$this->setAppLocale('en');
105+
106+
$this->registerRoute('route', 'en.route.name')->domain('en.domain.test');
107+
$this->registerRoute('route', 'nl.route.name')->domain('nl.domain.test');
108+
109+
$this->assertEquals('http://nl.domain.test/route', route('route.name', [], true, 'nl'));
110+
$this->assertEquals('http://nl.domain.test/route', route('en.route.name', [], true, 'nl'));
111+
$this->assertEquals('http://nl.domain.test/route', route('nl.route.name', [], true, 'nl'));
112+
}
113+
97114
/** @test */
98115
public function it_always_gets_the_url_of_a_localized_route_if_a_locale_is_specified()
99116
{
@@ -193,10 +210,10 @@ public function it_generates_a_signed_route_url_for_a_specific_locale()
193210
* @param string $name
194211
* @param \Closure|null $callback
195212
*
196-
* @return void
213+
* @return \Illuminate\Routing\Route
197214
*/
198215
protected function registerRoute($url, $name, $callback = null)
199216
{
200-
Route::name($name)->get($url, $callback ?: function () {});
217+
return Route::name($name)->get($url, $callback ?: function () {});
201218
}
202219
}

0 commit comments

Comments
 (0)