Skip to content

Commit

Permalink
Merge pull request #18 from lsmith77/referer
Browse files Browse the repository at this point in the history
make it possible to use referer to determine back() url
  • Loading branch information
denisdulici authored Mar 7, 2020
2 parents d82266a + 4d4d2a6 commit a8625f4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/Config/language.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@
*/
'url' => false,

/*
|--------------------------------------------------------------------------
| Set strategy
|--------------------------------------------------------------------------
|
| This option will determine the strategy used to determine the back url.
| It can be 'session' (default) or 'referer'
|
*/
'strategy' => 'session',

/*
|--------------------------------------------------------------------------
| Carbon Language
Expand Down
39 changes: 34 additions & 5 deletions src/Controllers/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ public function back($locale, Request $request)
{
$this->setLocale($locale, $request);

$url = config('language.strategy') === 'referer'
? $this->getUrlFromReferer($request)
: $this->getUrlFromSession($request);

return redirect(
$url
? $url
: (config('language.url') ? url('/' . $locale) : url('/'))
);
}

private function getUrlFromSession(Request $request)
{
$session = $request->session();

if (config('language.url')) {
Expand All @@ -72,10 +85,26 @@ public function back($locale, Request $request)

$session->setPreviousUrl($url);
}
return redirect(
$session->previousUrl()
? $session->previousUrl()
: (config('language.url') ? url('/' . $locale) : url('/'))
);

return $session->previousUrl();
}

private function getUrlFromReferer(Request $request)
{
$url = $request->headers->get('referer');

if (config('language.url')) {
$url = substr(str_replace(env('APP_URL'), '', $url), 7);

if (strlen($url) === 3) {
$url = substr($url, 3);
} else {
$url = substr($url, strrpos($url, '/') + 1);
}

$url = rtrim(env('APP_URL'), '/') . '/' . $locale . '/' . ltrim($url, '/');
}

return $url;
}
}

0 comments on commit a8625f4

Please sign in to comment.