Skip to content
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

Fixed redirect error without locale parameter #9

Merged
merged 1 commit into from
Jun 10, 2024
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
12 changes: 11 additions & 1 deletion src/Middlewares/LocalizationByParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
namespace LaravelLang\Routes\Middlewares;

use Illuminate\Http\Request;
use LaravelLang\Config\Facades\Config;

class LocalizationByParameter extends Middleware
{
protected function detect(Request $request): bool|float|int|string|null
{
return $request->route()->parameter($this->names()->parameter);
if ($this->present($request)) {
return $request->route()->parameter($this->names()->parameter);
}

return null;
}

protected function present(Request $request): bool
{
return in_array(Config::shared()->routes->names->parameter, $request->route()->parameterNames(), true);
}
}
9 changes: 8 additions & 1 deletion src/Middlewares/LocalizationByParameterWithRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use LaravelLang\Config\Facades\Config;
use LaravelLang\Locales\Facades\Locales;
use LaravelLang\Routes\Concerns\KeyNames;

use function array_merge;
use function in_array;
use function response;

class LocalizationByParameterWithRedirect extends Middleware
Expand All @@ -19,7 +21,7 @@ class LocalizationByParameterWithRedirect extends Middleware

public function __invoke(Request $request, Closure $next)
{
if ($this->invalidParameter($request) && ($name = $this->routeName($request))) {
if ($this->present($request) && $this->invalidParameter($request) && ($name = $this->routeName($request))) {
return $this->redirect($name, $this->parameters($request));
}

Expand All @@ -43,6 +45,11 @@ protected function hasParameter(Request $request): bool
return (bool) $request->route()?->hasParameter($this->names()->parameter);
}

protected function present(Request $request): bool
{
return in_array(Config::shared()->routes->names->parameter, $request->route()->parameterNames(), true);
}

protected function parameters(Request $request): array
{
return array_merge($request->route()?->parameters() ?? [], [
Expand Down
8 changes: 4 additions & 4 deletions tests/Concerns/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public function setUpRoutes(): void

app('router')
->middleware([
// LocalizationByParameter::class,
LocalizationByParameter::class,
LocalizationByParameterWithRedirect::class,
// LocalizationByHeader::class,
// LocalizationByCookie::class,
// LocalizationBySession::class,
LocalizationByHeader::class,
LocalizationByCookie::class,
LocalizationBySession::class,
])
->get('clean/{foo}', $this->jsonResponse())
->name('clean');
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/WithoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Tests\Constants\LocaleValue;

use function Pest\Laravel\getJson;

test('without locale passing', function () {
$foo = 'test';

getJson(route('clean', compact('foo')))
->assertSuccessful()
->assertJsonPath($foo, LocaleValue::TranslationFrench);

assertEventNotDispatched();
});