Skip to content

Properly restore locale after catching RouteNotFoundException #88

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 1 commit 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
24 changes: 14 additions & 10 deletions src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public function route($name, $parameters = [], $absolute = true, $locale = null)
App::setLocale($locale);
}

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

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

return $url;
Expand Down Expand Up @@ -66,11 +68,13 @@ public function signedRoute($name, $parameters = [], $expiration = null, $absolu
App::setLocale($locale);
}

$url = parent::signedRoute($resolvedName, $parameters, $expiration, $absolute);

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

return $url;
Expand Down
4 changes: 2 additions & 2 deletions src/LocalizedUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
use CodeZero\UrlBuilder\UrlBuilder;
use Illuminate\Support\Facades\URL;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Request;
use Illuminate\Contracts\Routing\UrlRoutable;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

class LocalizedUrlGenerator
{
Expand Down Expand Up @@ -118,7 +118,7 @@ protected function generateNamedRouteURL(string $locale, array $parameters = [],
{
try {
return URL::route($this->route->getName(), $parameters, $absolute, $locale);
} catch (InvalidArgumentException $e) {
} catch (RouteNotFoundException $e) {
return '';
}
}
Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/Illuminate/Routing/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,66 @@ public function it_generates_a_temporary_signed_route_url_for_a_specific_locale(
$this->get($expiredUrl)->assertSee('Expired Signature');
}

/** @test */
public function it_throws_a_route_not_found_exception_for_missing_route_names_when_generating_a_route_url()
{
$this->expectException(RouteNotFoundException::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 (RouteNotFoundException $exception) {}

$this->assertEquals('en', App::getLocale());
}

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

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

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

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

$this->assertEquals('en', App::getLocale());
}

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

URL::temporarySignedRoute('missing.route', now()->addMinutes(30));
}

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

try {
URL::temporarySignedRoute('missing.route', now()->addMinutes(30), [], true, 'nl');
} catch (RouteNotFoundException $exception) {}

$this->assertEquals('en', App::getLocale());
}

/** @test */
public function it_allows_routes_to_be_cached()
{
Expand Down