Skip to content

Commit 4aaf060

Browse files
committed
Improve unregistered route handling in the UrlGenerator
1 parent 72a5a36 commit 4aaf060

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/UrlGenerator.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ public function route($name, $parameters = [], $absolute = true, $locale = null)
4141
return parent::route($name, $parameters, $absolute);
4242
}
4343

44-
// Normalize the route name by removing any locale prefix.
45-
// We will prepend the applicable locale manually.
46-
$name = $this->stripLocaleFromRouteName($name);
47-
4844
// Cache the current locale so we can change it
4945
// to automatically resolve any translatable
5046
// route parameters such as slugs.
@@ -54,12 +50,27 @@ public function route($name, $parameters = [], $absolute = true, $locale = null)
5450
// as a prefix for the route name.
5551
$locale = $locale ?: $currentLocale;
5652

53+
// Normalize the route name by removing any locale prefix.
54+
// We will prepend the applicable locale manually.
55+
$baseName = $this->stripLocaleFromRouteName($name);
56+
57+
// If the route has a name (not just the locale prefix)
58+
// add the requested locale prefix.
59+
$newName = $baseName ? "{$locale}.{$baseName}" : '';
60+
61+
// If the new localized name does not exist, but the unprefixed route name does,
62+
// someone might be calling "route($name, [], true, $locale)" on a non localized route.
63+
// In that case, resolve the unprefixed route name.
64+
if (Route::has($baseName) && ! Route::has($newName)) {
65+
$newName = $baseName;
66+
}
67+
5768
// Update the current locale if needed.
5869
if ($locale !== $currentLocale) {
5970
App::setLocale($locale);
6071
}
6172

62-
$url = parent::route("{$locale}.{$name}", $parameters, $absolute);
73+
$url = parent::route($newName, $parameters, $absolute);
6374

6475
// Restore the current locale if needed.
6576
if ($locale !== $currentLocale) {

tests/Unit/UrlGeneratorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ public function it_always_gets_the_url_of_a_localized_route_if_a_locale_is_speci
123123
$this->assertEquals(url('nl/route'), route('route.name', [], true, 'nl'));
124124
}
125125

126+
/** @test */
127+
public function it_returns_a_registered_non_localized_url_if_a_localized_version_does_not_exist()
128+
{
129+
$this->setSupportedLocales(['en', 'nl']);
130+
$this->setAppLocale('en');
131+
132+
$this->registerRoute('route', 'route.name');
133+
$this->registerRoute('nl/route', 'nl.route.name');
134+
135+
$this->assertEquals(url('route'), route('route.name', [], true, 'en'));
136+
}
137+
126138
/** @test */
127139
public function it_throws_if_no_valid_route_can_be_found_for_the_given_locale()
128140
{
@@ -136,6 +148,19 @@ public function it_throws_if_no_valid_route_can_be_found_for_the_given_locale()
136148
route('en.route.name', [], true, 'nl');
137149
}
138150

151+
/** @test */
152+
public function it_throws_if_you_do_not_specify_a_name_for_a_localized_route()
153+
{
154+
$this->setSupportedLocales(['en', 'nl']);
155+
$this->setAppLocale('en');
156+
157+
$this->registerRoute('en/route', 'en.');
158+
159+
$this->expectException(InvalidArgumentException::class);
160+
161+
route('en.', [], true, 'en');
162+
}
163+
139164
/** @test */
140165
public function it_temporarily_changes_the_app_locale_when_generating_a_route_url()
141166
{

0 commit comments

Comments
 (0)