Skip to content

Commit

Permalink
Include grouped prefix in route key
Browse files Browse the repository at this point in the history
  • Loading branch information
chinleung committed Oct 27, 2023
1 parent e7dc649 commit 6f4aac7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
12 changes: 9 additions & 3 deletions src/MultilingualRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ protected function generateRoute(string $key, $handle, string $locale, array $op
$route = $this->router->addRoute(
$this->getRequestMethodFromOptions($options),
$this->applyUniqueRegistrationKey(
$this->generateUriFromKey($key, $locale),
$this->router->getLastGroupPrefix()
? $key
: $this->generateUriFromKey($key, $locale),
$locale
),
$handle ?: '\ChinLeung\MultilingualRoutes\Controllers\ViewController'
Expand Down Expand Up @@ -188,6 +190,12 @@ protected function finalizeRoute(Route $route, string $key, string $locale, arra

$this->applyConstraints($route, $locale, $options);

$route = $this->cleanUniqueRegistrationKey($route, $locale);

if ($this->router->getLastGroupPrefix()) {
$route->setUri($this->generateUriFromKey($route->uri, $locale));
}

if ($prefix = $this->generatePrefixForLocale($key, $locale)) {
$route->setUri("{$prefix}/{$route->uri}");
}
Expand All @@ -207,8 +215,6 @@ protected function finalizeRoute(Route $route, string $key, string $locale, arra
)
));

$route = $this->cleanUniqueRegistrationKey($route, $locale);

if (method_exists($route, 'setBindingFields')) {
$route->setBindingFields($bindingFields);
}
Expand Down
45 changes: 16 additions & 29 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,6 @@ public function a_multilingual_redirect_route_can_be_registered(): void
}
}

/** @test **/
public function a_multilingual_group_routes_with_prefix_can_be_registered_and_accessed(): void
{
$this->registerGroupedTestRoute();
foreach (locales() as $locale) {
$this->assertEquals(
route($locale.'.test'),
localized_route('test', [], $locale)
);
$this->assertNotNull(Route::getRoutes()->match(app(Request::class)->create(route($locale.'.test', [], false))));
}
}

/** @test **/
public function a_route_can_have_different_names_based_on_locales(): void
{
Expand Down Expand Up @@ -127,7 +114,7 @@ public function the_locale_name_has_priority_over_group_name(): void
public function it_can_limit_route_to_specific_locales(): void
{
$this->registerTestRoute()
->only(['fr']);
->only(['fr']);

$this->assertEquals(
route('fr.test'),
Expand All @@ -142,7 +129,7 @@ public function it_can_limit_route_to_specific_locales(): void
public function it_can_remove_specific_locales_from_route(): void
{
$this->registerTestRoute()
->except(['fr']);
->except(['fr']);

$this->assertEquals(
route('en.test'),
Expand All @@ -156,16 +143,17 @@ public function it_can_remove_specific_locales_from_route(): void
/** @test **/
public function the_default_locale_routes_can_be_prefixed(): void
{
config(['laravel-multilingual-routes.prefix_default' => true]);
config([
'laravel-multilingual-routes.prefix_default' => true,
]);

$this->registerTestRoute();

$this->assertEquals(
route('fr.test'),
$route = localized_route('test', [], 'fr')
);
$route = localized_route('test', [], 'fr');

$this->assertRegexp('/fr/', $route);
$this->assertEquals(route('fr.test'), $route);

$this->assertEquals(url('fr/teste'), $route);
}

/** @test **/
Expand Down Expand Up @@ -252,7 +240,11 @@ public function a_route_with_prefix_stack_can_be_registered(): void
});

$this->assertEquals(url('prefix/test'), localized_route('test'));
$this->assertEquals(url('fr/prefix/teste'), localized_route('test', [], 'fr'));

$this->assertEquals(
url('fr/prefixe/teste'),
localized_route('test', [], 'fr')
);
}

/** @test **/
Expand Down Expand Up @@ -561,20 +553,15 @@ protected function registerTestRedirectRoute(): MultilingualRoutePendingRegistra
return Route::multilingual('from')->redirect('to');
}

protected function registerGroupedTestRoute(): void
{
Route::prefix('test')->group(function () {
$this->registerTestRoute();
});
}

protected function registerTestTranslations(): void
{
$this->registerTranslations([
'en' => [
'routes.prefix/test' => 'prefix/test',
'routes.test' => 'test',
],
'fr' => [
'routes.prefix/test' => 'prefixe/teste',
'routes.test' => 'teste',
],
]);
Expand Down

0 comments on commit 6f4aac7

Please sign in to comment.