Skip to content

Properly restore locale after catching RouteNotFoundException #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"optimize-autoloader": true,
"allow-plugins": {
"0.0.0/composer-include-files": true,
"kylekatarnls/update-helper": true
"kylekatarnls/update-helper": true,
"codezero/composer-preload-files": true
}
},
"minimum-stability": "dev",
Expand Down
12 changes: 7 additions & 5 deletions src/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ public function route($name, $parameters = [], $absolute = true, $locale = null)
App::setLocale($locale);
}

$url = parent::route($newName, $parameters, $absolute);

// Restore the current locale if needed.
if ($locale !== $currentLocale) {
App::setLocale($currentLocale);
try {
$url = parent::route($newName, $parameters, $absolute);
} finally {
// Restore the current locale if needed.
if ($locale !== null && $locale !== $currentLocale) {
App::setLocale($currentLocale);
}
}

return $url;
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,25 @@ public function it_generates_a_signed_route_url_for_a_specific_locale()
$this->get($tamperedUrl)->assertSee('Invalid Signature');
}

/** @test */
public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_route_url()
{
$this->expectException(InvalidArgumentException::class);

URL::route('missing.route');
}

/** @test */
public function the_app_locale_is_correctly_restored_when_catching_a_route_not_found_exception_when_generating_a_route_url()
{
$this->setAppLocale('en');

try {
URL::route('missing.route', [], true, 'nl');
} catch (InvalidArgumentException $exception) {}

$this->assertEquals('en', App::getLocale());
}
/** @test */
public function it_allows_routes_to_be_cached()
{
Expand Down