jeudi 9 septembre 2021

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

Aucun commentaire:

Enregistrer un commentaire