Skip to content

Commit

Permalink
Merge pull request #9 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Fixed redirect error without `locale` parameter
  • Loading branch information
andrey-helldar authored Jun 10, 2024
2 parents f5a5bd8 + 8643822 commit 0598d5a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
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();
});

0 comments on commit 0598d5a

Please sign in to comment.