Skip to content

Commit 581e2e7

Browse files
committed
Map locales to custom (sub)domains
1 parent 877c4c7 commit 581e2e7

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/Macros/LocalizedRoutesMacro.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public static function register()
2323
$locales = Config::get('localized-routes.supported-locales', []);
2424
$omitPrefix = Config::get('localized-routes.omit_url_prefix_for_locale');
2525

26-
foreach ($locales as $locale) {
26+
foreach ($locales as $locale => $domain) {
27+
if (is_numeric($locale)) {
28+
$locale = $domain;
29+
$domain = null;
30+
}
31+
2732
// Change the current locale so we can
2833
// use it in the callback, for example
2934
// to register translated route URI's.
@@ -35,10 +40,14 @@ public static function register()
3540

3641
// Prefix the URL unless the locale
3742
// is configured to be omitted.
38-
if ($locale !== $omitPrefix) {
43+
if ($domain === null && $locale !== $omitPrefix) {
3944
$route->prefix($locale);
4045
}
4146

47+
if ($domain !== null) {
48+
$route->domain($domain);
49+
}
50+
4251
$route->group($callback);
4352
}
4453

tests/Unit/Macros/LocalizedRoutesMacroTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ public function it_registers_a_route_for_each_locale()
3434
});
3535

3636
$routes = $this->getRoutes();
37+
$domains = $routes->pluck('action.domain');
3738
$names = $routes->pluck('action.as');
3839
$uris = $routes->pluck('uri');
3940

41+
// Verify that no custom domains are registered.
42+
$this->assertTrue($domains->filter()->isEmpty());
43+
4044
$this->assertNotContains('route.name', $names);
4145
$this->assertContains('en.route.name', $names);
4246
$this->assertContains('nl.route.name', $names);
@@ -69,6 +73,32 @@ public function it_registers_a_root_route_for_each_locale()
6973
$this->assertContains('nl', $uris);
7074
}
7175

76+
/** @test */
77+
public function it_maps_a_custom_domain_to_each_locale()
78+
{
79+
$this->setAvailableLocales([
80+
'en' => 'english-domain.com',
81+
'nl' => 'dutch-domain.com',
82+
]);
83+
84+
Route::localized(function () {
85+
Route::get('/', function () {})
86+
->name('home');
87+
});
88+
89+
$routes = $this->getRoutes();
90+
91+
$route = $routes->first();
92+
$this->assertEquals('english-domain.com', $route->action['domain']);
93+
$this->assertEquals('en.home', $route->action['as']);
94+
$this->assertEquals('/', $route->uri);
95+
96+
$route = $routes->last();
97+
$this->assertEquals('dutch-domain.com', $route->action['domain']);
98+
$this->assertEquals('nl.home', $route->action['as']);
99+
$this->assertEquals('/', $route->uri);
100+
}
101+
72102
/** @test */
73103
public function it_registers_a_url_without_prefix_for_a_configured_main_locale()
74104
{

0 commit comments

Comments
 (0)