Skip to content

Commit 7d33ded

Browse files
committed
Handle root URL placeholder overriding registered routes (#28)
1 parent af7d62e commit 7d33ded

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Macros/LocalizedRoutesMacro.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public static function register()
2828
$setMiddleware = $options['use_locale_middleware']
2929
?? Config::get('localized-routes.use_locale_middleware', false);
3030

31+
if ($omitPrefix) {
32+
// Move the omitted locale to the end of the array
33+
// to avoid root placeholders catching existing slugs.
34+
// https://github.com/codezero-be/laravel-localized-routes/issues/28
35+
$locales = array_filter($locales, function ($locale) use ($omitPrefix) {
36+
return $locale !== $omitPrefix;
37+
});
38+
array_push($locales, $omitPrefix);
39+
}
40+
3141
foreach ($locales as $locale => $domain) {
3242
// Allow supported locales to be a
3343
// simple array of locales or an

tests/Unit/Macros/LocalizedRoutesMacroTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,40 @@ public function it_registers_a_url_without_prefix_for_a_configured_main_locale()
8282
$this->assertContains('nl/about', $uris);
8383
}
8484

85+
/** @test */
86+
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale()
87+
{
88+
$this->setSupportedLocales(['en', 'nl']);
89+
$this->setOmitUrlPrefixForLocale('en');
90+
$this->setUseLocaleMiddleware(true);
91+
92+
Route::localized(function () {
93+
Route::get('/', function () { return 'Home '.App::getLocale(); });
94+
Route::get('{slug}', function () { return 'Dynamic '.App::getLocale(); });
95+
});
96+
97+
$this->assertEquals(
98+
['nl', 'nl/{slug}', '/', '{slug}'],
99+
$this->getRoutes()->pluck('uri')->toArray()
100+
);
101+
102+
$response = $this->call('GET', '/');
103+
$response->assertOk();
104+
$this->assertEquals('Home en', $response->original);
105+
106+
$response = $this->call('GET', '/nl');
107+
$response->assertOk();
108+
$this->assertEquals('Home nl', $response->original);
109+
110+
$response = $this->call('GET', '/dynamic');
111+
$response->assertOk();
112+
$this->assertEquals('Dynamic en', $response->original);
113+
114+
$response = $this->call('GET', '/nl/dynamic');
115+
$response->assertOk();
116+
$this->assertEquals('Dynamic nl', $response->original);
117+
}
118+
85119
/** @test */
86120
public function it_maps_a_custom_domain_to_each_locale()
87121
{

0 commit comments

Comments
 (0)