diff --git a/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php b/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php index d043a94..9eb396b 100644 --- a/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php +++ b/src/Mcamara/LaravelLocalization/Middleware/LocaleCookieRedirect.php @@ -56,7 +56,7 @@ public function handle($request, Closure $next) $redirection = app('laravellocalization')->getLocalizedURL($locale); $redirectResponse = new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']); - return $redirectResponse->withCookie(cookie()->forever('locale', $params[0])); + return $redirectResponse->withCookie(cookie()->forever('locale', $locale)); } return $next($request); diff --git a/tests/LocalizerTests.php b/tests/LocalizerTests.php index 80f83f6..828b2bf 100644 --- a/tests/LocalizerTests.php +++ b/tests/LocalizerTests.php @@ -880,4 +880,38 @@ public function testSetLocaleWithMapping() $this->assertEquals('http://localhost/custom/some-route', app('laravellocalization')->localizeURL('some-route', 'custom')); $this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en')); } + + public function testRedirectWithHiddenDefaultLocaleInUrlAndSavedLocale() + { + app('router')->group([ + 'prefix' => app('laravellocalization')->setLocale(), + 'middleware' => [ + 'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter', + 'Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect', + ], + ], function (){ + app('router')->get('/', ['as'=> 'index', function () { + return 'Index page'; + }, ]); + }); + + app('config')->set('laravellocalization.hideDefaultLocaleInURL', true); + + $currentLocale = 'es'; + + $crawler = $this->call( + 'GET', + $this->test_url, + [], + ['locale' => $currentLocale], + [], + [] + ); + + $this->assertResponseStatus(302); + $this->assertRedirectedTo($this->test_url2 . '/es'); + + $localeCookie = $crawler->headers->getCookies()[0]; + $this->assertEquals($currentLocale, $localeCookie->getValue()); + } }