How to set up locale and redirection logic Laravel 5

I’m new to laravel and looking for advice on how to structurally set up some functionality.

My application uses the ipInfo api to identify the visitors country via ip, then redirects the user to the appropriate locale (e.g. /gb/, /ca/ etc…).

I only want to make a single call to ipInfo per request, but since the visitor is redirected, I’m struggling to cut the calls down to one (from two).

Initially, I had the logic within a serviceprovider boot method, but I decided using session variables was a good idea for keeping track of the visitor – and since sessions are not (normally) available in a service provider, I moved the logic to custom middleware.

This middleware is called after sessionStart middleware and for the most part works well. However, I’ve now noticed that this middleware is not being triggered for every request which I think may have something to do with caching? This means that the locale redirect logic is not always triggered and the user is sometimes not redirected.

This must be a normal feature in many laravel applications, so how should this be done? Bear in mind that I want the redirection/locale logic to remain as fast as possible, so I don’t want this logic to occur within a view.

*Side-note, I’m using statamic as a framework on top of laravel 5.

Thanks!

public function handle($request, Closure $next)
{
$uri = $request->server->get('REQUEST_URI');

if (
env('APP_ENV') != 'dev' &&
request()->server->get('REQUEST_METHOD') === 'GET' &&
!in_array('img', request()->segments()) &&
!in_array('_debugbar', request()->segments())) {

if (!Session::has('from_redirect')) {
$user_real_country = $this->getUserCountry();
Session::put('user_country', $user_real_country);
} else {
$user_real_country = Session::get('user_country');
Session::forget('from_redirect');
Session::save();
}

if (Request::input('orc') && in_array(Request::input('orc'), explode(',', env('ORC_OPTIONS')))) {
$country = Request::input('orc');
if (Cookie::has('country')) {
Cookie::forget('country');
}
} elseif (Cookie::has('country')) {
$country = Cookie::get('country');
} else {
$country = $user_real_country;
}

if (!is_null($country)) {
Cookie::put('country', $country);
$this->setLocale($country);
$this->setuser_Locale($user_real_country);
}
}

if (site_locale() == 'en-GB' && substr($uri, 0, 3) != '/gb') {
Session::put('from_redirect', true);
if(substr($uri, 0, 3) == '/ca') {
return redirect(Config::getSiteUrl() . substr($uri, 4));
}
return redirect(Config::getSiteUrl() . ltrim($uri, '/'));
} elseif (site_locale() == 'en-CA' && substr($uri, 0, 3) != '/ca') {
Session::put('from_redirect', true);
if(substr($uri, 0, 3) == '/gb') {
return redirect(Config::getSiteUrl() . substr($uri, 4));
}
return redirect(Config::getSiteUrl() . ltrim($uri, '/'));
} elseif(site_locale() == 'en' && (substr($uri, 0, 3) == '/ca' || substr($uri, 0, 3) == '/gb')) {
Session::put('from_redirect', true);
return redirect(Config::getSiteUrl() . substr($uri, 4));
}

return $next($request);
}

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/3l7uXBX
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.