Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: codezero-be/laravel-localized-routes
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: codezero-be/laravel-localized-routes
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2.x
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Apr 6, 2023

  1. Copy the full SHA
    2fbc95f View commit details
Showing with 28 additions and 6 deletions.
  1. +2 −1 composer.json
  2. +7 −5 src/UrlGenerator.php
  3. +19 −0 tests/Unit/UrlGeneratorTest.php
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
12 changes: 7 additions & 5 deletions src/UrlGenerator.php
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 19 additions & 0 deletions tests/Unit/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -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()
{