Skip to content

Commit fa242a9

Browse files
Merge pull request #16 from Laravel-Lang/1.x
Fixed route redirection
2 parents 9d6219d + 7fdea8b commit fa242a9

4 files changed

+60
-4
lines changed

src/Middlewares/LocalizationByParameterPrefix.php

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Http\Request;
88
use Illuminate\Support\Arr;
99
use Illuminate\Support\Str;
10+
use LaravelLang\Config\Facades\Config;
11+
use LaravelLang\Locales\Facades\Locales;
1012
use LaravelLang\Routes\Helpers\Route;
1113

1214
class LocalizationByParameterPrefix extends LocalizationByParameterWithRedirect
@@ -27,8 +29,30 @@ protected function routeName(Request $request): ?string
2729
return null;
2830
}
2931

32+
protected function invalidParameter(Request $request): ?bool
33+
{
34+
return ! $this->hasParameter($request)
35+
|| ! $this->trim($this->detect($request))
36+
|| ! $this->isInstalled($this->detect($request))
37+
|| $this->isDefault($this->detect($request));
38+
}
39+
40+
protected function isDefault(bool|float|int|string|null $locale): bool
41+
{
42+
if ($this->allowRedirect()) {
43+
return Locales::info($locale)->code === Locales::getDefault()->code;
44+
}
45+
46+
return false;
47+
}
48+
3049
protected function routePrefix(): string
3150
{
3251
return Route::prefix();
3352
}
53+
54+
protected function allowRedirect(): bool
55+
{
56+
return Config::shared()->routes->redirect;
57+
}
3458
}

src/Middlewares/LocalizationByParameterWithRedirect.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ protected function defaultLocale(): string
7171
return Locales::getDefault()->code;
7272
}
7373

74-
protected function isInstalled(bool|float|int|string|null $parameter): bool
74+
protected function isInstalled(bool|float|int|string|null $locale): bool
7575
{
76-
return Locales::isInstalled((string) $parameter);
76+
return Locales::isInstalled((string) $locale);
7777
}
7878
}

tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use LaravelLang\Config\Enums\Name;
56
use LaravelLang\Routes\Helpers\Route as RouteName;
67
use Tests\Constants\LocaleValue;
78

@@ -15,7 +16,22 @@
1516
->assertJsonPath($foo, LocaleValue::TranslationFrench);
1617
});
1718

18-
test('main locale', function (string $locale) {
19+
test('main locale with enabled redirect', function (string $locale) {
20+
config()->set(Name::Shared() . '.routes.redirect_default', true);
21+
22+
$foo = 'test';
23+
24+
getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale')))
25+
->assertRedirectToRoute('via.group.facade', [
26+
'foo' => $foo,
27+
]);
28+
29+
assertEventNotDispatched();
30+
})->with('main-locales');
31+
32+
test('main locale with disabled redirect', function (string $locale) {
33+
config()->set(Name::Shared() . '.routes.redirect_default', false);
34+
1935
$foo = 'test';
2036

2137
getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale')))

tests/Feature/Middlewares/ParameterPrefixForMacroTest.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use LaravelLang\Config\Enums\Name;
56
use LaravelLang\Routes\Helpers\Route as RouteName;
67
use Tests\Constants\LocaleValue;
78

@@ -15,7 +16,22 @@
1516
->assertJsonPath($foo, LocaleValue::TranslationFrench);
1617
});
1718

18-
test('main locale', function (string $locale) {
19+
test('main locale with enabled redirect', function (string $locale) {
20+
config()->set(Name::Shared() . '.routes.redirect_default', true);
21+
22+
$foo = 'test';
23+
24+
getJson(route(RouteName::prefix() . 'via.group.macro', compact('foo', 'locale')))
25+
->assertRedirectToRoute('via.group.macro', [
26+
'foo' => $foo,
27+
]);
28+
29+
assertEventNotDispatched();
30+
})->with('main-locales');
31+
32+
test('main locale with disabled redirect', function (string $locale) {
33+
config()->set(Name::Shared() . '.routes.redirect_default', false);
34+
1935
$foo = 'test';
2036

2137
getJson(route(RouteName::prefix() . 'via.group.macro', compact('foo', 'locale')))

0 commit comments

Comments
 (0)