Skip to content

Commit

Permalink
Merge pull request #16 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Fixed route redirection
  • Loading branch information
andrey-helldar authored Jun 17, 2024
2 parents 9d6219d + 7fdea8b commit fa242a9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/Middlewares/LocalizationByParameterPrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use LaravelLang\Config\Facades\Config;
use LaravelLang\Locales\Facades\Locales;
use LaravelLang\Routes\Helpers\Route;

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

protected function invalidParameter(Request $request): ?bool
{
return ! $this->hasParameter($request)
|| ! $this->trim($this->detect($request))
|| ! $this->isInstalled($this->detect($request))
|| $this->isDefault($this->detect($request));
}

protected function isDefault(bool|float|int|string|null $locale): bool
{
if ($this->allowRedirect()) {
return Locales::info($locale)->code === Locales::getDefault()->code;
}

return false;
}

protected function routePrefix(): string
{
return Route::prefix();
}

protected function allowRedirect(): bool
{
return Config::shared()->routes->redirect;
}
}
4 changes: 2 additions & 2 deletions src/Middlewares/LocalizationByParameterWithRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ protected function defaultLocale(): string
return Locales::getDefault()->code;
}

protected function isInstalled(bool|float|int|string|null $parameter): bool
protected function isInstalled(bool|float|int|string|null $locale): bool
{
return Locales::isInstalled((string) $parameter);
return Locales::isInstalled((string) $locale);
}
}
18 changes: 17 additions & 1 deletion tests/Feature/Middlewares/ParameterPrefixForFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use LaravelLang\Config\Enums\Name;
use LaravelLang\Routes\Helpers\Route as RouteName;
use Tests\Constants\LocaleValue;

Expand All @@ -15,7 +16,22 @@
->assertJsonPath($foo, LocaleValue::TranslationFrench);
});

test('main locale', function (string $locale) {
test('main locale with enabled redirect', function (string $locale) {
config()->set(Name::Shared() . '.routes.redirect_default', true);

$foo = 'test';

getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale')))
->assertRedirectToRoute('via.group.facade', [
'foo' => $foo,
]);

assertEventNotDispatched();
})->with('main-locales');

test('main locale with disabled redirect', function (string $locale) {
config()->set(Name::Shared() . '.routes.redirect_default', false);

$foo = 'test';

getJson(route(RouteName::prefix() . 'via.group.facade', compact('foo', 'locale')))
Expand Down
18 changes: 17 additions & 1 deletion tests/Feature/Middlewares/ParameterPrefixForMacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use LaravelLang\Config\Enums\Name;
use LaravelLang\Routes\Helpers\Route as RouteName;
use Tests\Constants\LocaleValue;

Expand All @@ -15,7 +16,22 @@
->assertJsonPath($foo, LocaleValue::TranslationFrench);
});

test('main locale', function (string $locale) {
test('main locale with enabled redirect', function (string $locale) {
config()->set(Name::Shared() . '.routes.redirect_default', true);

$foo = 'test';

getJson(route(RouteName::prefix() . 'via.group.macro', compact('foo', 'locale')))
->assertRedirectToRoute('via.group.macro', [
'foo' => $foo,
]);

assertEventNotDispatched();
})->with('main-locales');

test('main locale with disabled redirect', function (string $locale) {
config()->set(Name::Shared() . '.routes.redirect_default', false);

$foo = 'test';

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

0 comments on commit fa242a9

Please sign in to comment.