From 56962e45557d7e8300ce3ecf18a8dba52933a05b Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 23 Jun 2016 21:00:17 +0000 Subject: [PATCH] Updating the middleware * Using the status code 302 for language redirections. * Fix the bug that redirects always to default locale. --- src/Bases/Middleware.php | 4 +--- src/Middleware/LocalizationRedirect.php | 16 +++++++--------- src/Middleware/TranslationRedirect.php | 2 +- tests/Middleware/LocaleCookieRedirectTest.php | 2 +- tests/Middleware/LocaleSessionRedirectTest.php | 2 +- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Bases/Middleware.php b/src/Bases/Middleware.php index 34cb809..556016b 100644 --- a/src/Bases/Middleware.php +++ b/src/Bases/Middleware.php @@ -105,8 +105,6 @@ protected function getLocalizedRedirect($locale) */ protected function makeRedirectResponse($url, $code = 302) { - return new RedirectResponse($url, $code, [ - 'Vary' => 'Accept-Language' - ]); + return new RedirectResponse($url, $code, ['Vary' => 'Accept-Language']); } } diff --git a/src/Middleware/LocalizationRedirect.php b/src/Middleware/LocalizationRedirect.php index fa676ce..a819531 100644 --- a/src/Middleware/LocalizationRedirect.php +++ b/src/Middleware/LocalizationRedirect.php @@ -28,15 +28,11 @@ class LocalizationRedirect extends Middleware */ public function handle(Request $request, Closure $next) { - $locale = $request->segment(1, null); - - if ( - $redirectUrl = $this->getRedirectionUrl($locale) - ) { + if ($redirectUrl = $this->getRedirectionUrl($request)) { // Save any flashed data for redirect session()->reflash(); - return $this->makeRedirectResponse($redirectUrl, 301); + return $this->makeRedirectResponse($redirectUrl); } return $next($request); @@ -49,12 +45,14 @@ public function handle(Request $request, Closure $next) /** * Get redirection. * - * @param string $locale + * @param \Illuminate\Http\Request $request * * @return string|false */ - protected function getRedirectionUrl($locale) + protected function getRedirectionUrl(Request $request) { + $locale = $request->segment(1, null); + if ($this->getSupportedLocales()->has($locale)) { return $this->isDefaultLocaleHidden($locale) ? localization()->getNonLocalizedURL() @@ -67,7 +65,7 @@ protected function getRedirectionUrl($locale) $this->getCurrentLocale() !== $this->getDefaultLocale() || ! $this->hideDefaultLocaleInURL() ) { - return localization()->getLocalizedURL(); + return localization()->getLocalizedURL(session('locale'), $request->path()); } return false; diff --git a/src/Middleware/TranslationRedirect.php b/src/Middleware/TranslationRedirect.php index b91091f..0ec00b2 100644 --- a/src/Middleware/TranslationRedirect.php +++ b/src/Middleware/TranslationRedirect.php @@ -36,7 +36,7 @@ public function handle(Request $request, Closure $next) $translatedUrl = $this->getTranslatedUrl($request); if ( ! is_null($translatedUrl)) { - return $this->makeRedirectResponse($translatedUrl, 301); + return $this->makeRedirectResponse($translatedUrl); } return $next($request); diff --git a/tests/Middleware/LocaleCookieRedirectTest.php b/tests/Middleware/LocaleCookieRedirectTest.php index 8163692..9730011 100644 --- a/tests/Middleware/LocaleCookieRedirectTest.php +++ b/tests/Middleware/LocaleCookieRedirectTest.php @@ -42,7 +42,7 @@ public function it_can_pass_redirect_without_cookie() /** @var RedirectResponse $response */ $response = $this->call('GET', $this->testUrlOne); - $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals(302, $response->getStatusCode()); $this->assertEquals($this->testUrlOne . 'en', $response->getTargetUrl()); } } diff --git a/tests/Middleware/LocaleSessionRedirectTest.php b/tests/Middleware/LocaleSessionRedirectTest.php index de337d5..fecb7fc 100644 --- a/tests/Middleware/LocaleSessionRedirectTest.php +++ b/tests/Middleware/LocaleSessionRedirectTest.php @@ -45,7 +45,7 @@ public function it_can_pass_redirect_without_session() /** @var RedirectResponse $response */ $response = $this->call('GET', $this->testUrlOne); - $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals(302, $response->getStatusCode()); $this->assertEquals($this->testUrlOne . 'en', $response->getTargetUrl()); } }