Skip to content

Commit

Permalink
Merge pull request #50 from ARCANEDEV/patch-1
Browse files Browse the repository at this point in the history
Updating the middleware
  • Loading branch information
arcanedev-maroc authored Jun 23, 2016
2 parents 8024cdb + 56962e4 commit 29f188f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/Bases/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
16 changes: 7 additions & 9 deletions src/Middleware/LocalizationRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/TranslationRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Middleware/LocaleCookieRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion tests/Middleware/LocaleSessionRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 29f188f

Please sign in to comment.